How to Convert an Array Into a Generic Type in C#

The following are the topics to be exlained in this article:

  1. Convert an array to a list
  2. Convert list to an array
  3. Convert an array to a dictionary
  4. Convert dictionary to an array
  5. Convert list to a dictionary
  6. Convert dictionary to a list

Let's say we have a class called “Student” with three auto-implemented properties.

properties

In the same cs file, we have another class called “MainProgram” in which there is a Main method.

cs file

Convert an array to a list

To convert an array to a list the first thing we need to do is to create an array and here we will create an array of type Student.

convert an array to a list

The next step is to retrieve the items from this array and for that we can use a foreach loop.

Convert an array to a list

Run the application.

Convert an array to a list

Let's see how to convert this array of Students into a list of Students.

To convert an array to a List we can use the ToList() extension method in the System.Linq namespace.

So, we can say StudentArray.ToList(

Convert an array to a list

Look at the return type of this ToList method, it is returning a List<Student> object back which means we can create an object of that type to store the data of the array we created.

Convert an array to a list

To get all the student details from StudentList, we can use a foreach loop.

Convert an array to a list

Run the application.

Convert an array to a list

Convert List to an Array

To convert a list to an array, we can use the ToArray() extension method in the System.Linq namespace.

Convert List to an Array

To loop through each item in array, we can use foreach loop.

Convert List to an Array

Run the application.

Convert List to an Array

Convert an Array to a Dictionary

To convert an array to a dictionary, we can use the ToDictionary() extension method in the System.Linq namespace.

So, we can say StudentArray.ToDictonary(

Convert an Array to a Dictionary

Look the parameter this method expects. The first parameter expects a key and the second parameter expects a value and as we know a dictionary is a collection of key/value pairs. So, we need to pass the name of the object from where this dictionary will get the key and the value and for that we will use a lambda expression.

Convert an Array to a Dictionary

Now look at the return type of this ToDictionary method.

Convert an Array to a Dictionary

It is returning a dictionary object back and look at the type of the key/value pair <int, Student> which means we can create an object of that type and store that data we got from the ToDictionary method.

Convert an Array to a Dictionary

The next step is to retrieve the student details from this StudentDictionary object and for that we can use a foreach loop.

As we know a dictionary is a collection of key/value pairs. So, we can say: 

foreach (KeyValuePair<int, Student> student in StudentDictionary) {  
}

Because the dictionary object will return a key and the value back.

Convert an Array to a Dictionary

Run the application.

Convert an Array to a Dictionary

Convert Dictionary to an Array

To convert a dictionary to an array, we can use the ToArray extension method. But before converting the dictionary collection into an array, first we need to retrieve the values from the collection of the dictionary object and for that we can use the Values property and on that property we can invoke the ToArray method.

Convert Dictionary to an Array

Convert Dictionary to an Array

To get all the student details we can use a foreach loop.

Convert Dictionary to an Array

Run the application.

Convert Dictionary to an Array

Convert List to a Dictionary

In the topic Convert Array to List, we have created a StudentList student object. Let's see how to convert this StudentList into a dictionary.

To convert a list into a dictionary, we can use the ToDictionary method.

Convert List to a Dictionary

In the first parameter of this ToDictionary method, we need to pass the object from where we will get the key and in the second parameter we need to pass the object from where we will get the value.

Based on the key/value we pass in as the parameter argument, the return type of ToDictionary is assigned and here it returns a dictionary object of <int, Student>. So, we can create an object that type and store the data.

To get all the student details from this dictionary object, we can use a foreach loop.

Convert List to a Dictionary

Run the application.

Convert List to a Dictionary

Convert Dictionary to a List

To convert a dictionary into a list, we can use the ToList method. In the topic Convert an array to a Dictionary we have created a StudentDictionary object. So, let' see how to convert that object into a list.

Convert Dictionary to a List

Run the application.

Convert Dictionary to a List

I hope you like this. Thank you.


Similar Articles