Create generics lists in C#.Net

This C#.Net tutorial, we will discuss, how to generics lists in C#.Net.

Generics allow you to define a class with placeholders for the type of its fields, methods, parameters, etc. Generics replace these placeholders with some specific type at compile time.

Create generics lists in C#.Net

We can create list objects by invoking the default constructor. In generic class List <T>we need to specify the type for the values of the list with the declaration.

Ex: var intList = new List<int>();

The default constructor creates an empty list. As soon as elements are added to the list, the capacity of the list is extended to allow four elements. If the fifth element is added, the list is resized to allow eight elements. If eight elements are not enough, the list is resized again to contain 16 elements. With every resizes the capacity of the list is doubled.

But you can also define the size while declaration like below:

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

But programmatically also you can set or get the size like below:

Set Size:
intList.Capacity = 20;

Get Size:
int size = intList.Capacity;

Also you can initialize a list like below:

var intList = new List<int>() {1, 2};

You can add elements to a list by using the Add method:

var intList = new List<int>();
intList.Add(1);
intList.Add(2);

You may like following Asp.net C#.net tutorials:

Hope this C#.net tutorial helps to learn how to create generics lists in C#.Net

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>