Conversion From List to Array and Vice-Versa

Introduction

If I want to use the list element as an array element then it is necessary to convert the list into an array, so in this article I convert the list into an array and array into the list. In this article I also explain how to find the maximum element in the list and how to find the minimum element in list.

Convert List to array

In this you can see how to convert your List to an array, using the integer element type, this example creates a new List and populates it with some integers. The List is a constructed type and can only hold integer values. Next it  uses ToArray on the List.

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of list<>

            List<int> number = new List<int>();

            //add the elements in the list

            number.Add(10);

            number.Add(20);

            number.Add(15);

            number.Add(25);

            number.Add(30);

            number.Add(35);

            number.Add(40);

            number.Add(45);

            //creation of an array

            int[] a = number.ToArray();

            //display the array element

            Console.WriteLine("the array elements are:");

            foreach (int i in a)

            {

                Console.WriteLine(i);

            }

        }

    }

}

Output

output11.jpg

Convert array to List

In this example we see  how to convert an array of any number of elements to a List that has the same type of elements. First, I initialize a new string[] array containing 7 strings. Next it converts the array to a List with the new List constructor. 

 

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            //create array

            string[] s = new string[] { "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" };

            //create list

            List<string> days = new List<string>(s);

            Console.WriteLine("The list element is:");

            foreach (string i in days)

            {

                Console.WriteLine(i);

            }

        }

    }

}

 

Output

 

output12.jpg

Find maximum element

Write the following code to find the maximum element in the list. For this we use the max() method of the list.

 

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            List<int> a = new List<int>();

            a.Add(10);

            a.Add(20);

            a.Add(1);

            a.Add(30);

            a.Add(67);

            Console.WriteLine("list elements are:");

            foreach (int i in a)

            {

                Console.WriteLine(i);

            }

            int max = a.Max();

            Console.WriteLine("Maximum element in the list is:" + max);

        }

    }

}

 

Output

output22.jpg

 

Find minimum element

Write the following code to find the minimum element in the list. For this we use the min() method of the list.

 

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            List<int> a = new List<int>();

            a.Add(10);

            a.Add(20);

            a.Add(1);

            a.Add(30);

            a.Add(67);

            Console.WriteLine("list elements are:");

            foreach (int i in a)

            {

                Console.WriteLine(i);

            }

            int min = a.Min();

            Console.WriteLine("Minimum element in the list is:" + min);

        }

    }

}

 

Output

 minimum.jpg

 

Summary: In this article I explain how to convert the array into a list and a list into an array and also to find the maximum and minimum number from the list.

Up Next
    Ebook Download
    View all
    Learn
    View all