Generic List constructors in C#

Hi Friend,

In the below post we will learn about various constructors available to create a generic List Object.

The C# List<T> represents a resizable strongly type List of object of type “T”. Lets see how we can use it to create a strongly type list of integers.The List<T> class provide three constructors which we can use to create a strongly type list object. Lets see it one by one.

 List<int> marksList = new List<int>();

In the above code we are creating a generic list of integer using constructor without any parameters.

List<int> marksList = new List<int>(10);

In the above code we are creating a generic list of integer using constructor which takes capacity as a parameter. The property ‘Capacity’ is the limit at which List can allocate elements without reallocation of new memory block. If the ‘Capacity’ or you try to add more element than its ‘Capacity’, a reallocation happens for the new memory block. If you have provided capacity then the reallocation process creates a new List of the same specified type for the given capacity.Otherwise In the second case it doubles the capacity. Then it use Array.Copy method to copy existing elements to the new strongly typed List.Lets see the second case with an example.

using System;
using System.Collections.Generic;

namespace GenericListSample
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> marksList = new List<int>(4);
            Console.WriteLine("Initial Capacity :{0}", marksList.Capacity);
            // Adding element till its capacity.
            marksList.Add(12);
            marksList.Add(14);
            marksList.Add(18);
            marksList.Add(15);
            Console.WriteLine("Capacity after adding 4 elements :{0}", marksList.Capacity);
            // Add one more element so that the capacity < count.
            marksList.Add(88);
            // Now again check the capacity
            Console.WriteLine("Capacity after adding 5th element :{0}", marksList.Capacity);
            Console.ReadLine();
        }
    }
}

In the above code we have created a strongly List of integer with 4 capacity, then we have added 4 elements to it. after that we trying to add one more element to it and in the output we can see that it doubles the capacity results ‘8’. The output is as below

List1

If you have a predefined array of same type and you want to convert it to the generic List collection then you can use third constructor as below.

 

 List<int> marksList = new List<int>(new int[] { 12, 14, 18, 15 });

The above code is taking an array of integer as input and we are creating a strongly type List Object of Integers.

In the above post we have learned about how to declare a generic list object of a specified type and how to add elements to it.

Hope this helps you. 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s