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.

retrieve the items

Run the application.

Run
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(

StudentArray

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.

ToList method

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

StudentList

Run the application.

Run the application
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.

foreach loop

Run the application.

output
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(
ToDictonary
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.

dictionary

Now look at the return type of this ToDictionary method.

ToDictionary

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.

create an object

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:

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

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

dictionary object

Run the application.

program output
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

ToArray method

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

use foreach loop

Run the application.

Run application
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.

student details

Run the application.

Run program
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.

StudentDictionary

Run the application.

List
I hope you like this. Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all