Convert Array to List in C#
-
Convert an Array to a List With the
Array.ToList()
Method Inside Linq inC#
-
Convert an Array to a List With the
List.AddRange()
Method inC#
This tutorial will discuss methods to convert an array to a list in C#.
Convert an Array to a List With the Array.ToList()
Method Inside Linq in C#
The Linq or language integrated query is used for fast text manipulation in C#. The Array.ToList()
method inside the Linq can convert an array to a list. The Array.ToList()
method converts the calling array to a list and returns the result in a list data structure. The following code example shows us how to convert an array to a list with the Array.ToList()
method inside the Linq in C#.
using System;
using System.Collections.Generic;
using System.Linq;
namespace convert_array_to_list
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 10, 12, 13 };
List<int> lst = arr.ToList();
foreach (var element in lst)
{
Console.WriteLine(element);
}
}
}
}
Output:
10
12
13
Convert an Array to a List With the List.AddRange()
Method in C#
The List.AddRange()
method is used to insert a range of values inside a list in C#. The List.AddRange()
inserts the element of any data structure inside the calling list. The following code example shows us how to convert an array to a list with the List.AddRange()
function in C#.
using System;
using System.Collections.Generic;
using System.Linq;
namespace convert_array_to_list
{
class Program
{
static void Main(string[] args)
{
int[] arr = { 10, 20, 30 };
List<int> lst = new List<int>();
lst.AddRange(arr);
foreach (var element in arr)
{
Console.WriteLine(element);
}
}
}
}
Output:
10
20
30