Conversion Between Array List and Dictionary in C#

Conversion between Array, List, and Dictionary in C#

In this article, we will discuss how to perform Conversion Between Array List and Dictionary in C#. Please read our previous article where we discussed Dictionary in C# with examples. As part of this article, we will discuss the following six things.

  1. Convert an array to a List – Use ToList() method
  2. Convert a list to an array – Use ToArray() method
  3. Convert a List to a Dictionary – Use ToDictionary() method
  4. Convert an array to a Dictionary – Use ToDictionary() method
  5. Convert a Dictionary to an array – Use ToArray() method on the Values Property of the dictionary object
  6. Convert a Dictionary to a List – Use the ToList() method on the Values Property of the dictionary object
Let us understand this with an example.

The code is self-explained. Please go through the comments.

using System;
using System.Collections.Generic;
using System.Linq;
namespace CollectionDemo
{
    public class Program
    {
        static void Main(string[] args)
        {
            //Create Employee object
            Employee emp1 = new Employee()
            {
                ID = 101,
                Name = "Pranaya",
                Gender = "Male",
                Salary = 20000
            };
            Employee emp2 = new Employee()
            {
                ID = 102,
                Name = "Priyanka",
                Gender = "Female",
                Salary = 30000
            };
            Employee emp3 = new Employee()
            {
                ID = 103,
                Name = "Anurag",
                Gender = "Male",
                Salary = 40000
            };

            // Create an array of employees with size 3
            // and then Store the 3 employees into the array
            Employee[] arrayEmployees = new Employee[3];
            arrayEmployees[0] = emp1;
            arrayEmployees[1] = emp2;
            arrayEmployees[2] = emp3;

            // To convert an array to a List, use ToList() method
            List<Employee> listEmployees = arrayEmployees.ToList();
            foreach (Employee emp in listEmployees)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Gender ={2}, Salary = {3}",
                               emp.ID, emp.Name, emp.Gender, emp.Salary);
            }
            Console.WriteLine();

            // To convert a List to an array, use ToArray() method
            Employee[] arrayAllEmployeesFromList = listEmployees.ToArray();
            foreach (Employee emp in arrayAllEmployeesFromList)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Gender ={2}, Salary = {3}",
                               emp.ID, emp.Name, emp.Gender, emp.Salary);
            }
            Console.WriteLine();

            // To convert a List to a Dictionary, use ToDictionary() method
            Dictionary<int, Employee> dictionaryEmployees = listEmployees.ToDictionary(employee => employee.ID, employee => employee);
            foreach (KeyValuePair<int, Employee> keyValuePairEmployees in dictionaryEmployees)
            {
                Console.WriteLine("Key = {0}", keyValuePairEmployees.Key);
                Employee emp = keyValuePairEmployees.Value;
                Console.WriteLine("ID = {0}, Name = {1}, Gender ={2}, Salary = {3}",
                               emp.ID, emp.Name, emp.Gender, emp.Salary);
            }
            Console.WriteLine();


            // To convert an Array to a Dictionary, use ToDictionary() method
            Dictionary<int, Employee> dictionaryEmployeesFromArray = arrayEmployees.ToDictionary(employee => employee.ID, employee => employee);
            // Loop thru the dictionary and print the key/value pairs
            foreach (KeyValuePair<int, Employee> kvp in dictionaryEmployeesFromArray)
            {
                Console.WriteLine("Key = {0}", kvp.Key);
                Employee emp = kvp.Value;
                Console.WriteLine("ID = {0}, Name = {1}, Gender ={2}, Salary = {3}",
                               emp.ID, emp.Name, emp.Gender, emp.Salary);
            }

            // To Convert a dictionaty to an array, use ToArray method on the Values 
            // Peoperty of the dictionary object
            Employee[] arrayAllEmployeesFromDictionary = dictionaryEmployeesFromArray.Values.ToArray();
            foreach (Employee emp in arrayAllEmployeesFromDictionary)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Gender ={2}, Salary = {3}",
                               emp.ID, emp.Name, emp.Gender, emp.Salary);
            }
            Console.WriteLine();

            // To Convert a dictionary to a List, use To List method on the Values
            // Property of the dictionary object
            List<Employee> listAllEmployeesFromDictionary = dictionaryEmployeesFromArray.Values.ToList();
            foreach (Employee emp in listAllEmployeesFromDictionary)
            {
                Console.WriteLine("ID = {0}, Name = {1}, Gender ={2}, Salary = {3}",
                               emp.ID, emp.Name, emp.Gender, emp.Salary);
            }

            Console.ReadKey();
        }
    }
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Salary { get; set; }
    }
}
Output:

Conversion between Array List and Dictionary in C#

In the next article, I am going to discuss List vs Dictionary in C# with examples. Here, in this article, I try to explain the Conversion Between Array List and Dictionary in C# with an example. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *