Quick Reach
- 1 What is the List in C#?
- 2 How to create a C# list?
- 3 A list example with integer elements
- 4 A list with string example
- 5 The list insert method to add element example
- 6 Accessing specific element in the list example
- 7 The list with the foreach loop example
- 8 The list sort example
- 9 C# list Find method example
- 10 Related
What is the List in C#?
The list is a collection of elements or variables in a specified order. The difference between array and list is that the list is dynamically sized whereas arrays are fixed.
A few main points about the C# list are:
- A list class of C# is a collection of variables.
- The list <T> is defined in the System.Collections.Generic namespace.
- The list <T> is the strongly typed list of objects.
- The T in the list <T> defines the type of elements in the list like integers, strings etc.
- The elements of list C# are accessible by the integer-based index.
- Just like arrays, the index of the list starts at 0.
- The list is useful when you have no idea that how many elements an array will contain.
- The list class provides methods and properties to add, remove, sort elements in the list.
- A very large list <T> object, in the 64-bit system, can contain as much as 2 billion elements.
How to create a C# list?
This is how you can create an integer list:
List<int> tstlist = new List<int>();
As we have the basic idea what list are and how to create it, now let us look at a few examples of using the list in the C# programs. We will also look at a few useful methods of the list after examples.
A list example with integer elements
In this example, we will create a list with integer elements. We will create four elements and then display it by using a for loop. See code and output by clicking the link below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<int> tstlist = new List<int>();
tstlist.Add(10);
tstlist.Add(20);
tstlist.Add(30);
tstlist.Add(40);
for (int i = 0; i < tstlist.Count; i++)
{
Console.WriteLine(tstlist[i]);
}
Console.ReadLine();
}
}
}
|
You can see, four elements are shown when we run this example by pressing F5 (in our console based created project). Two methods of the list, add and count are used in the example.
The C# list add method is used to add elements in the list. While C# count property is used in the for loop to get the total count of elements in the list, that acts as the maximum value in the for loop. The count list property returns the total count of elements in the list.
A list with string example
Following is an example of creating a list of string elements. Again we have created a four elements list that will be displayed by using the for loop. See example and output by clicking the link or image below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<string> strlist = new List<string>();
strlist.Add(“This”);
strlist.Add(“is”);
strlist.Add(“C#”);
strlist.Add(“List”);
for (int i = 0; i < strlist.Count; i++)
{
Console.WriteLine(strlist[i]);
}
Console.ReadLine();
}
}
}
|
You can see, the list elements are added by using the add list method. After creating the elements, we used a for loop and count method to display the list elements.
The list insert method to add element example
You can use list insert method to add an element at a specific position. Following example shows how to do that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<int> tstlist = new List<int>();
tstlist.Add(10);
tstlist.Add(20);
tstlist.Add(30);
tstlist.Add(40);
Console.WriteLine(“List before insert method!”);
for (int i = 0; i < tstlist.Count; i++)
{
Console.WriteLine(tstlist[i]);
}
tstlist.Insert(3, 35);
Console.WriteLine(“List after insert method!”);
for (int i = 0; i < tstlist.Count; i++)
{
Console.WriteLine(tstlist[i]);
}
Console.ReadLine();
}
}
}
|
You can see, a list is created with four elements. After displaying list elements by using a for loop, we added an element at the index 3 by using the insert method. The list is displayed again with added elements.
Accessing specific element in the list example
You can access a specific element by using the integer-based index number starting from zero. Following example shows that how to access a specific element in the list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<int> tstlist = new List<int>();
tstlist.Add(10);
tstlist.Add(20);
tstlist.Add(30);
tstlist.Add(40);
//Accessing 3rd elements in list
Console.WriteLine(tstlist[2]);
Console.ReadLine();
}
}
}
|
You can see, the 3rd element is accessed by using [2], as such index starts at 0.
The list with the foreach loop example
In above examples, we used for loop to display items of the list. You can use a foreach loop to iterate through the list as well. Following is an example of the C# list with the foreach loop example. See code and output by clicking the link below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<int> tstlist = new List<int>();
tstlist.Add(10);
tstlist.Add(20);
tstlist.Add(30);
tstlist.Add(40);
foreach (int i in tstlist)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
|
The list sort example
Following is a list sort example. The sort method will sort the list elements. For this example, we have created a list with unordered element values.
First, we displayed the list elements in the original order. After that, we used the list sort method and displayed the list again by using the foreach loop.
Note that, the sort method will sort the list from lower to the higher number for numeric list while sort method will sort alphabetically for the string list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<int> tstlist = new List<int>();
tstlist.Add(70);
tstlist.Add(50);
tstlist.Add(20);
tstlist.Add(40);
Console.WriteLine(“list before sort”);
foreach (int i in tstlist)
{
Console.WriteLine(i);
}
Console.WriteLine(“list after sort”);
tstlist.Sort();
foreach (int i in tstlist)
{
Console.WriteLine(i); // C# List example
}
Console.ReadLine();
}
}
}
|
You can see, unsorted and sorted list after using the list sort C# method.
C# list Find method example
The list Find method is used to search in the list. The Find method returns the first occurrence of the matched condition in the given list. See the following example of using list find
See the following example of using list find method where we have four elements list. After that, we searched the list by using the list find method to return element greater than the value of 10.
See the code and output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System;
using System.Collections.Generic;
namespace csharp_demo
{
class Program
{
static void Main(string[] args)
{
List<int> tstlist = new List<int>();
tstlist.Add(5);
tstlist.Add(70);
tstlist.Add(50);
tstlist.Add(20);
tstlist.Add(40);
int x = tstlist.Find(lstfind => lstfind > 10);
Console.WriteLine(“The first occurance by using c# find is: “ + x);
Console.ReadLine();
}
}
}
|
As you can see, the list find method returned 70 value, which is the first element and is greater than 10.
Further Reading: Array of C# | The ArrayList
Leave A Comment?