Open In App

How to Convert Integer Array to List in C#?

Last Updated : 28 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

We have given a integer array arr and the task is to convert integer array into the list lst in C#. In order to do this task, we have the following methods:

Method 1:  List<T> class: This represents the list of objects which can be accessed by index. It comes under the System.Collection.Generic namespace. List<T> class also provides the methods to search, sort, and manipulate lists. And this is also used to convert a given an integer array to the list .

Syntax:

List<int> lst = new List<int> { 1, 2, 3};

Example:

C#




// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
    
    static void Main(string[] args)
    {
        // given integer array 
        // { 10, 20, 30, 40, 50 }
          
        // using List<T> class
        List<int> lst = new List<int> { 10, 20, 30, 40, 50 };
        
          // you can write the above line of code as
          // int[] ints = new [] { 10, 20, 30, 40, 50 };
        // List<int> lst = ints.OfType<int>().ToList();
        
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
    }
}


Output:

10 20 30 40 50

Method 2:  List<T>(IEnumerable<T>) Constructor : Using this constructor a new instance of the List<T> class can be  initialize that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. Thus this can also be used to convert a given an integer array to the list .

Syntax:

List<int> lst = new List<int>(integer_array);

Example:

C#




// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List<int> get_list(int[] arr) 
    {
        // List<T>(IEnumerable<T>) 
        // Constructor
        // is a used to convert a 
        // given an integer array 
        // to the list
      
        List<int> L = new List<int> (arr);
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List<int> lst = get_list(arr);
          
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
    }
}


Output:

10 20 30 40 50

Method 3:  AddRange(IEnumerable<T>) Method : This method is used to add the elements of the specified collection to the end of the List<T>. Thus this can also be used to convert a given an integer array to the list.

Syntax:

List<int> lst = new List<int>();
lst.AddRange(integer_array)

Example:

C#




// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List<int> get_list(int[] arr) 
    {
        // AddRange(IEnumerable<T>) 
        // Method is a used to convert
        // given an integer array 
        // to the list
      
        List<int> L = new List<int>();
        L.AddRange(arr);
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List<int> lst = get_list(arr);
          
        // printing output
           foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
  
  
    }
}


Output:

10 20 30 40 50

Method 4:  ToList() Method: The Enumerate.Tolist method is from System.Linq Namespace which creates a List<T> from an IEnumerable<T>. It returns a List<T> that contains elements from the input sequences.

Syntax:

List<int> lst = integer_array.ToList();

Example:

C#




// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List<int> get_list(int[] arr) 
    {
        // ToList() Method is a used 
        // to convert a given an 
        // integer array to the list
      
        List<int> L = arr.ToList();
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List<int> lst = get_list(arr);
          
        // printing output
          foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
    }
}


Output:

10 20 30 40 50

Method 5:  Add() Method : This method is used to add an object to the end of the List. Thus this can also be used to convert a given an integer array to the list .

Syntax:

List<int> lst = new List<int>();
lst.Add(int val);

Example:

C#




// C# program to convert a 
// given an integer array 
// to the list
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
  
public class GFG{
      
    static List<int> get_list(int[] arr) 
    {
        // Add() Method is a used 
        // to convert a given an 
        // integer array to the list
      
        List<int> L = new List<int>();
          
        for(int i = 0 ; i < arr.Length ; i++)
        {
            L.Add(arr[i]);
        }
          
        return L;
    }
    
    static void Main(string[] args)
    {
        // given integer array
        int[] arr = { 10, 20, 30, 40, 50 };
          
        // function calling
        List<int> lst = get_list(arr);
          
        // printing output
        foreach (int i in lst)
        {
          Console.Write(i + " ");
        }
    }
}


Output:

10 20 30 40 50


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads