The following are the topics to be exlained in this article:
- Convert an array to a list
- Convert list to an array
- Convert an array to a dictionary
- Convert dictionary to an array
- Convert list to a dictionary
- Convert dictionary to a list
Let's say we have a class called “Student” with three auto-implemented properties.
In the same cs file, we have another class called “MainProgram” in which there is a Main method.
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.
The next step is to retrieve the items from this array and for that we can use a foreach loop.
Run the application.
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(
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.
To get all the student details from StudentList, we can use a foreach loop.
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.
To loop through each item in array, we can use foreach loop.
Run the application.
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(
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.
Now look at the return type of this ToDictionary method.
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.
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.
Run the application.
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.
To get all the student details we can use a foreach loop.
Run the 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.
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.
Run the application.
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.
Run the application.
I hope you like this. Thank you.