You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$10
For Premium members:
Overview Of List Collection Class In C#.
Before beginning the explanation, please read and understand the collection class in .NET.
Example for the List<T> class is as follows,
As you can see in the above example, I have declared and initialized a list of strings in _days variable and I am adding items in the list using Add() method. Using foreach loop, I am displaying all the elements in the list.
Here, you can add elements any time, at any index position, no matter the list size. For example, if you wanted to add “Wednesday” at third position, then you can use Insert(index, item) method. Here, the third position,i.e., list is zero index based so third position=3-1=2, as shown below.
I have explained about the List of string items. Here, it's not only string but you can have any objects like int, double, class, etc.
Let’s see for the list of class items, as shown below.
In the above image, you can see that I have marked 2 in a red rectangle that is the initial size of list, but you can see here also that I have added 3 items, which are out of range. If it is an array, then it will throw an out of range exception as shown below.
There are some useful methods in List<T> class object, as shown below.
Programming List with C#