Generic Collection Best Practices-1

				
					var result = GetProducts();//list
var result1 = GetProductsArray();//array
var result2 = GetProductsDict();//dict

//list and array --> ILIST...
//dic --> Idiction..
//Ilist and Idic --> Icollection
Display(result);
Display(result1);
Display(result2.Values);


Console.WriteLine("test");
bool result12 = false;
System.Console.WriteLine(bool.TryParse("Off", out result12));


static void Display(ICollection<Product> products)
{
    foreach (var item in products)
    {
        Console.WriteLine(item.Price);
    }
}


/*IList...IDictionary,,Icollection....IEnumerable....
 * 
 *     List<>
 * 
 * **/
static IList<Product> GetProducts()
{
    return new List<Product>
    {
        new Product{ ID =1,Price=12},
        new Product{ ID =2,Price=22},
        new Product{ ID =3,Price=32},
    };
}


static Product[] GetProductsArray()
{
    return new Product[]
    {
        new Product{ ID =1,Price=12},
        new Product{ ID =2,Price=22},
        new Product{ ID =3,Price=32},
    };
}

static Dictionary<int, Product> GetProductsDict()
{
    return new Dictionary<int, Product>
    {
        {1, new Product{ ID =1,Price=12}},
        {2, new Product{ ID =1,Price=12}},
        {3, new Product{ ID =1,Price=12}},
    };
}



class Product
{
    public int ID { get; set; }
    public double Price { get; set; }
}
				
			

Leave a Comment