How to Convert Integer Array to List in C#?
Last Updated :
28 May, 2020
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#
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static void Main( string [] args)
{
List< int > lst = new List< int > { 10, 20, 30, 40, 50 };
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#
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List< int > get_list( int [] arr)
{
List< int > L = new List< int > (arr);
return L;
}
static void Main( string [] args)
{
int [] arr = { 10, 20, 30, 40, 50 };
List< int > lst = get_list(arr);
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#
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List< int > get_list( int [] arr)
{
List< int > L = new List< int >();
L.AddRange(arr);
return L;
}
static void Main( string [] args)
{
int [] arr = { 10, 20, 30, 40, 50 };
List< int > lst = get_list(arr);
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#
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List< int > get_list( int [] arr)
{
List< int > L = arr.ToList();
return L;
}
static void Main( string [] args)
{
int [] arr = { 10, 20, 30, 40, 50 };
List< int > lst = get_list(arr);
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#
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
public class GFG{
static List< int > get_list( int [] arr)
{
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)
{
int [] arr = { 10, 20, 30, 40, 50 };
List< int > lst = get_list(arr);
foreach ( int i in lst)
{
Console.Write(i + " " );
}
}
}
|
Output:
10 20 30 40 50
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...