• How to convert Array to List in C#

    Posted on February 19, 2013 by in Dotnet Tips & Tricks, Tips & Tricks

    public static void ConvertArrayToList()
    {
        Product[] products = new Product[3];
    
        Product product = new Product();
        product.ProductID = 680;
        product.ProductName = "HL Road Frame - Black, 58";
        product.ProductSubCategoryName = "Brakes";
        products[0] = product;
    
        product = new Product();
        product.ProductID = 706;
        product.ProductName = "HL Road Frame - Red, 58";
        product.ProductSubCategoryName = "Road Bikes";
        products[1] = product;
    
        product = new Product();
        product.ProductID = 749;
        product.ProductName = "Road-150 Red, 62";
        product.ProductSubCategoryName = "Road Bikes";
        products[2] = product;
    
        List<Product> productList = products.ToList();
    
         //iterate through product groups and print product attributes
       foreach (var prd in productList)
       {
           Console.WriteLine("SubCategoryName:\t" + prd.ProductSubCategoryName);
           Console.WriteLine("Product ID:\t\t" + prd.ProductID);
           Console.WriteLine("Product Name:\t\t" + prd.ProductName);
           Console.WriteLine();
       }
    }

    convert array to list 1

    Be Sociable, Share!
      Post Tagged with , , ,

    Written by

    View all articles by

    Email : [email protected]

    Leave a Reply