When we talk about any collection in C# then first thing which comes in our mind is Lists, they are used to create collection of any type, for example: We can create a list of integers, strings or any other complex type. Lists are present in System.Collections.Generic namespace, now the objects which are added or present in List can be accessed by their index. The other important part about Lists is that they grow in size automatically.
Now we will talk about how we can create a List in C#:
As mentioned above the Lists are present in System.Collections.Generic namespace so first we have to add this to access them, now the syntax for Lists is
List<string> Cars = new List<string> (3);
Now in the above example as we can see we have created a list of cars with capacity 3 and between the angular braces we have defined the type of list i.e. string.
For adding the elements to the List:
Cars.Add(“Volvo”);
Cars.Add(“Hyundai”);
Cars.Add(“BMW”);
Now if we have to Remove any item from the List by its index then:
Cars.RemoveAt(2);
So by above shown example we can Create, Add or Remove items in our List and also as the List are collection of items we can apply Foreach loop over it to loop over our items in the collection.