• How to convert ArrayList to List in C#

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

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    public static void ConvertArrayListToList()
    {
        ArrayList productsArrayList = new ArrayList();
    
        Product product = new Product();
        product.ProductID = 680;
        product.ProductName = "HL Road Frame - Black, 58";
        product.ProductSubCategoryName = "Brakes";
        productsArrayList.Add(product);
    
        product = new Product();
        product.ProductID = 706;
        product.ProductName = "HL Road Frame - Red, 58";
        product.ProductSubCategoryName = "Road Bikes";
        productsArrayList.Add(product);
    
        product = new Product();
        product.ProductID = 749;
        product.ProductName = "Road-150 Red, 62";
        product.ProductSubCategoryName = "Road Bikes";
        productsArrayList.Add(product);
    
        List<Product> productList = productsArrayList.Cast<Product>().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 arraylist to list 1

    Be Sociable, Share!
      Post Tagged with , , ,

    Written by

    View all articles by

    Email : [email protected]

    Leave a Reply