In this article, we are going to talk more about Generic List and Dictionary in C#. A List<T>
and Dictionary
are very useful collections in C#, and we are going to discover its features in the rest of the article.
If you want to see complete navigation of this tutorial, you can do that here C# Intermediate Tutorial.
To download the source code, you can visit the Generic List and Dictionary in C# Source Code.
We are going to split this article into the following sections:
List<T>
A List<T>
represents a strongly typed collection of objects that can be accessed by index.
To instantiate a List<T>
we need to provide a type between the angle brackets:
1 2 |
List<int> numberList = new List<int>(); List<Student> students = new List<Student>(); |
It has two more constructors that we can use to initialize a List object. With the first one, we can set initial capacity:
1 |
List<int> numbers = new List<int>(2); |
With the second one, we can populate our list with the IEnumerable collection:
1 2 |
int[] nums = new int[5] { 1, 2, 3, 4, 5 }; List<int> numbers = new List<int>(nums); |
To access any element we can specify its index position:
1 |
int number = numbers[1]; |
Methods and Properties
The Add
method adds the element inside a list:
1 2 3 4 5 6 7 8 9 |
List<int> numbers = new List<int>(); numbers.Add(34); numbers.Add(58); numbers.Add(69); foreach (int number in numbers) { Console.WriteLine(number); } |
AddRange
adds the elements of the specified collection to the end of a list:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<int> numbers = new List<int>(); numbers.Add(34); numbers.Add(58); numbers.Add(69); int[] nums = new int[] { 1, 22, 44 }; numbers.AddRange(nums); foreach (int number in numbers) { Console.WriteLine(number); } |
Contains
determines whether an element exists in the list:
1 2 3 4 |
if(numbers.Contains(34)) { Console.WriteLine("The number 34 exists in a list"); } |
The IndexOf
method returns the position of an element as an integer number. If an element couldn’t be found, this method returns -1:
1 2 3 4 5 |
int index; if((index = numbers.IndexOf(58)) != -1) { Console.WriteLine($"The number 58 is on the index: {index}"); } |
LastIndexOf
is similar to a previous method except it returns a last occurrence of the element.
CopyTo
method copies the entire collection to a compatible array, starting from the beginning of that array:
1 2 3 4 5 6 7 8 |
int[] copyArray = new int[6]; numbers.CopyTo(copyArray); foreach (int copyNumber in copyArray) { Console.WriteLine(copyNumber); } |
The Remove
method removes the first occurrence of a specific element from the list:
1 |
numbers.Remove(69); |
The Clear method clears all the elements from a list:
1 |
numbers.Clear(); |
We can check how many elements a list has by using the Count
property:
1 |
Console.WriteLine(numbers.Count); |
Dictionary
Dictionary
represents a collection of keys and values. To instantiate an object we can use the following syntax:
1 |
Dictionary<KeyType, ValueType> Name = new Dictionary<KeyType, ValueType>(); |
The KeyType represents a type for our key in a collection. The ValueType represents the value assigned to the key. So, we can extract our value from a collection by using the key inside the square brackets:
1 |
DictionaryName[key]; |
Dictionary has several constructors we can use to instantiate objects:
1 2 3 4 5 |
Dictionary<string, int> dictExample = new Dictionary<string, int>(); Dictionary<string, int> dictExample1 = new Dictionary<string, int>(5); //to set initial size Dictionary<string, int> dictExample2 = new Dictionary<string, int>(dictExample1); //accepts all the elements from created Key-Value collection |
Methods and Properties
The Add
method adds the key-value pair inside a collection:
1 2 3 4 5 6 7 8 9 10 |
Dictionary<string, int> dictExample = new Dictionary<string, int>(); dictExample.Add("First", 100); dictExample.Add("Second", 200); dictExample.Add("Third", 300); foreach (var item in dictExample) { Console.WriteLine(dictExample[item.Key]); } |
Remove
removes the key-value pair from a collection based on the specified key:
1 2 3 4 5 |
dictExample.Remove("Second"); foreach (var item in dictExample) { Console.WriteLine(dictExample[item.Key]); } |
ContainsKey
determines if a collection contains a specific key.
ContainsValue
determines if a collection contains a specific value:
1 2 3 4 5 6 7 8 9 |
if(dictExample.ContainsKey("First")) { Console.WriteLine("It contains key"); } if(dictExample.ContainsValue(300)) { Console.WriteLine("It contains value"); } |
The Clear
method removes all key-value pairs from a collection:
1 |
dictExample.Clear(); |
If we want to count all of our elements inside a collection, we can use the Count
property. If we want to get a collection of containing Keys
or containing Values
from a dictionary, we can use the Keys
and Values
properties:
1 2 3 4 5 6 7 8 9 10 11 |
Console.WriteLine(dictExample.Count); foreach (var key in dictExample.Keys) { Console.WriteLine(key); } foreach (var value in dictExample.Values) { Console.WriteLine(value); } |
Conclusion
In this article, we have learned:
- To use the List<T> collection with its methods
- To use a Dictionary with its methods and properties
In the next article, we are going to talk about Delegates in C#.
If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.