Types of Collections in C#

Hey pals, here we are to talk about collections. There are many types of collections in C#, like Arrays, Array List, Dictionaries, and List. Here we are going to talk about these four collections. Array is the most basic type of collection which you can use to store data of the same type, and once you declare the size of arrays you can never increase it during run time. Once you do that, then you’re going to lose all values inside the Arrays. There are certain disadvantages of using Arrays Collections due to which we may not prefer using array.

Another Collection is Array List. As the name suggests it’s also an array but it’s the array of lists. The basic difference between Array List and Arrays is that you can also increase the value of the Arrays List at any time, meaning that you can put any Value and it’ll automatically increase in size without losing your your Data or Array List. This is quite a different and a good thing, that you don’t need to limit your data by size. Instead you can add as many values as you want to.

Talking more about collections, we have another type of collection which is dictionaries, in which you can also store the values. I must add, you can store multiple values at the same time. In Dictionaries you assign a key value from one of the properties of your Data type, for example, if you have a class member Car then you can use one property as the key word for your Dictionary and in the other parameter you just pass the value of that object. For example,

  1. Car myCar = new Car();  
  2. mycar.make = “Nissan”  
  3. myCar.model = “Aitmax”  
And now you’re going to add in the object of Dictionary  myDictionary.add(myCar.make, myCar);

It’s just tha simple, and then using the key of the Dictionary you can also access all members of that class, or let’s say, all properties of class.

Now we’ll talk about the List. Lists are the most generic type of collections. You just don’t need to worry about the limit or size of values that you want to insert in a list. But what matters is the type of the list. Lists are very easy to declare. Just as you declare by using a new variable for array, it's almost the sam. 

For example:

List<Name of Class here> _List Name = new Now IntelliSense of Visual Studio will help you and suggestto you what you are most likely to type. And now, for adding student, you’ll use the name of list and add command and then you will pass the object name for which object you want to pass in the list.

And you can simply access all the members in the list by creating an object of student in your list name and placing it unde the for loop. Here is the sample code for your understanding.
  1. List<Student> myList = new List<Student>();  
  2. myList.Add(st1);  
  3. myList.Add(st2);  
  4. myList.Add(dp1);  
  5.   
  6. foreach (Student o in myList)  
  7. Console.WriteLine(o.name);  
This is what I talked about in the paragraph. I know it’s just a little boring to study the whole paragraph so from now on I’ll also be pasting code and images, so that if someone doesn't understand he can get to the direct code according to his/her understanding.

Furthermore, you can refer to the pictures of code attached and for even more reference you can refer to Microsoft Virtual Academy (Fundamentals of C#)and go to the Video tutorial of Robert Bob Taybor, and there you can refer to lecture 23 for a more clear understanding.

Kindly find the link attached for video tutorials