A List is a Collection that holds multiple values, of different Data Types. And in a List the elements are ordered (We will explain it soon) and allows duplicate values.
using System.Collections.Generic; public class MyApplication { public static void Main(string[] args) { var x = new List<object>(){5, "John", "C#"}; foreach (var data in x) { System.Console.WriteLine(data); } } }
So, in the above code we have created a List named x,
var x = new List<object>(){5, "John", "C#"};
To use List, we need to include the package System.Collections.Generic.
using System.Collections.Generic;
And put an Integer type value (i.e. 5) and two String type value (i.e. John and C#)
var x = new List<object>(){5, "John", "C#"};
So, to insert values of different data type, we have used object inside the angular brackets.
And initialised to the variable x.
So, we can see that two different data types are assigned to a List.
In the next line we have printed the List using foreach loop.
System.Console.WriteLine(data);
And we get the below output.
So, in the above code we have created a List with 2 different Datatype(i.e. String and Integer).
Now, what if we want to restrict the List to accept only a String or an Integer or a Float.
Let's see in the below example.
using System.Collections.Generic; public class MyApplication { public static void Main(string[] args) { var x = new List<string>(){5, "John", "C#"}; foreach (var data in x) { System.Console.WriteLine(data); } } }
So, what we have done is, created a List with three values, 5, John and C#.
var x = new List<string>(){5, "John", "C#"};
And at the time of initialisation, we have ended up with an error.
error CS1503: Argument 1: cannot convert from 'int' to 'string'
This is because we have restricted the List to accept only values of String datatype.
And the values we are trying to use contains an integer value i.e. 5.
Now, let us try inserting all String values.
using System.Collections.Generic; public class MyApplication { public static void Main(string[] args) { var x = new List<string>(){"Tom", "John", "C#"}; foreach (var data in x) { System.Console.WriteLine(data); } } }
Now, if you look at the output. We got the correct entries printed.
So far we have seen a List with default values. Now let us see, how can we create an empty List and assign values to it.
using System.Collections.Generic; public class MyApplication { public static void Main(string[] args) { var x = new List<string>(); x.Add("Tom"); x.Add("John"); x.Add("C#"); foreach (var data in x) { System.Console.WriteLine(data); } } }
So, in the above code, we have defined an empty List,
var x = new List<string>();
Then used the Add() method of the List to add values of String type to the List.
x.Add("Tom"); x.Add("John"); x.Add("C#");
Then we have printed the output using the foreach loop.
So, we have used foreach loop to get the values printed.
Let us see in detail, how foreach loop works.
using System.Collections.Generic; public class MyApplication { public static void Main(string[] args) { var x = new List<object>(){5, "John", "C#"}; foreach (var data in x) { System.Console.WriteLine(data); } } }
Similarly, in the above code we have created a List,
var x = new List<object>(){5, "John", "C#"};
And initialised to the variable x.
In the next line we have used the foreach loop to Iterate through the List.
foreach (var data in x) { System.Console.WriteLine(data); }
Now, if we see the iterations of for loop,
foreach (var data in x) { System.Console.WriteLine(data); }
In the first Iteration the first value of the List x (i.e. 5) is taken and put into the variable data.
And the print statement, prints the value of data.
Similarly, in the second Iteration the second value of the List x (i.e. John) is taken and put into the variable data.
And the print statement, prints the value of data.
Similarly, in the third Iteration the third value of the List x (i.e. C#) is taken and put into the variable data.
And the print statement, prints the value of data.
Now, if you see the final output. You can find that the values of the List are displayed in the same way they were inserted.
i.e. First 5 is printed, then the name John and finally C# is printed.
And if you see the List,
var x = new List<object>(){5, "John", "C#"};
It is printed in the same order it was inserted. And this is why a List is said to be Ordered.
Next, let us see, how to access the elements of the List in the next tutorial.