Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating a Generic List in .Net

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 79
    Comment on it

    Generic list are the list of object capable of storing any type, including Complex type such as class. It can even store a List of Items as well.

    Generic list is present in System.Collections.Generic Namespace in .Net frame work.

    As every type in .Net inherits directly or indirectly from System.Objects, so by creating a List of Object we can store any type in List.

    Example :

    Creating a Customer Class

    public class Customer
    {
        public int ID { get; set; }
        public string CustomerName { get; set; }
        public int Address { get; set; }
        public int PhoneNo { get; set; }
    }
    

    Now we create a list of Objects :

    public static List<List<object>> GetGerenicList()
    {
                List<List<object>> list = new List<List<object>>(); // Creating a List of List of object
                List<object> list1 = new List<object>();
                list1.Add("CompanyName1");
                list1.Add(101);
                Customer customarObj1 = new Customer();
                list1.Add(customarObj1);
                list1.Add(112.350);
                list.Add(list1); // adding a list of object to our List
                List<object> list2 = new List<object>();
                list1.Add("CompanyName2");
                list1.Add(102);
                Customer customarObj2 = new Customer();
                list1.Add(customarObj2);
                list1.Add(345.45);
                list.Add(list2); // adding a list of object to our List
    
               return list;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: