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 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:

It has two more constructors that we can use to initialize a List object. With the first one, we can set initial capacity:

With the second one, we can populate our list with the IEnumerable collection:

To access any element we can specify its index position:

Methods and Properties

The Add method adds the element inside a list:

AddRange adds the elements of the specified collection to the end of a list:

Contains determines whether an element exists in the 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:

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:

The Remove method removes the first occurrence of a specific element from the list:

The Clear method clears all the elements from a list:

We can check how many elements a list has by using the Count property:

Dictionary

Dictionary represents a collection of keys and values. To instantiate an object we can use the following syntax:

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:

Dictionary has several constructors we can use to instantiate objects:

Methods and Properties

The Add method adds the key-value pair inside a collection:

Remove removes the key-value pair from a collection based on the specified key:

ContainsKey determines if a collection contains a specific key.

ContainsValue determines if a collection contains a specific value:

The Clear method removes all key-value pairs from a collection:

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:

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.