Last night I had dream that the following was impossible. But in the same dream, someone from SO told me otherwise. Hence I would like to know if it it possible to convert System.Array to List

Array ints = Array.CreateInstance(typeof(int), 5);ints.SetValue(10, 0);ints.SetValue(20, 1);ints.SetValue(10, 2);ints.SetValue(34, 3);ints.SetValue(113, 4);

to

List<int> lst = ints.OfType<int>(); // not working
12

Best Answer


Save yourself some pain...

using System.Linq;int[] ints = new [] { 10, 20, 10, 34, 113 };List<int> lst = ints.OfType<int>().ToList(); // this isn't going to be fast.

Can also just...

List<int> lst = new List<int> { 10, 20, 10, 34, 113 };

or...

List<int> lst = new List<int>();lst.Add(10);lst.Add(20);lst.Add(10);lst.Add(34);lst.Add(113);

or...

List<int> lst = new List<int>(new int[] { 10, 20, 10, 34, 113 });

or...

var lst = new List<int>();lst.AddRange(new int[] { 10, 20, 10, 34, 113 });

Converting a C# array into a list can be done using the ToList() method provided by the System.Linq namespace. This method allows you to easily convert an array into a list without having to manually iterate through each element.

To convert an array into a list, you first need to import the System.Linq namespace. Then, you can use the ToList() method by passing the array as the parameter. The method will return a list containing all the elements from the array.

Here's an example:

int[] myArray = { 1, 2, 3, 4, 5 };List<int> myList = myArray.ToList();

In this example, the myArray array is converted into a list using the ToList() method. The resulting myList variable will contain the same elements as the original array.

Converting an array into a list can be useful in various scenarios. Lists provide additional functionality and flexibility compared to arrays, such as dynamic resizing and built-in methods for sorting and searching elements.

There is also a constructor overload for List that will work... But I guess this would required a strong typed array.

//public List(IEnumerable<T> collection)var intArray = new[] { 1, 2, 3, 4, 5 };var list = new List<int>(intArray);

... for Array class

var intArray = Array.CreateInstance(typeof(int), 5);for (int i = 0; i < 5; i++)intArray.SetValue(i, i);var list = new List<int>((int[])intArray);

Interestingly no one answers the question, OP isn't using a strongly typed int[] but an Array.

You have to cast the Array to what it actually is, an int[], then you can use ToList:

List<int> intList = ((int[])ints).ToList();

Note that Enumerable.ToList calls the list constructor that first checks if the argument can be casted to ICollection<T>(which an array implements), then it will use the more efficient ICollection<T>.CopyTo method instead of enumerating the sequence.

The simplest method is:

int[] ints = new [] { 10, 20, 10, 34, 113 };List<int> lst = ints.ToList();

or

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

In the case you want to return an array of enums as a list you can do the following.

using System.Linq;public List<DayOfWeek> DaysOfWeek{get{return Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>().ToList();}}

You can do like this basically:

int[] ints = new[] { 10, 20, 10, 34, 113 };

this is your array, and than you can call your new list like this:

 var newList = new List<int>(ints);

You can do this for complex object too.

in vb.net just do this

mylist.addrange(intsArray)

or

Dim mylist As New List(Of Integer)(intsArray)

You can just give it try to your code:

Array ints = Array.CreateInstance(typeof(int), 5);ints.SetValue(10, 0);ints.SetValue(20, 1);ints.SetValue(10, 2);ints.SetValue(34, 3);ints.SetValue(113, 4);int[] anyVariable=(int[])ints;

Then you can just use the anyVariable as your code.

I know two methods:

List<int> myList1 = new List<int>(myArray);

Or,

List<int> myList2 = myArray.ToList();

I'm assuming you know about data types and will change the types as you please.

Just use the existing method.. .ToList();

 List<int> listArray = array.ToList();

KISS(KEEP IT SIMPLE SIR)

I hope this is helpful.

enum TESTENUM{T1 = 0,T2 = 1,T3 = 2,T4 = 3}

get string value

string enumValueString = "T1";List<string> stringValueList = typeof(TESTENUM).GetEnumValues().Cast<object>().Select(m => Convert.ToString(m)).ToList();if(!stringValueList.Exists(m => m == enumValueString)){throw new Exception("cannot find type");}TESTENUM testEnumValueConvertString;Enum.TryParse<TESTENUM>(enumValueString, out testEnumValueConvertString);

get integer value

 int enumValueInt = 1;List<int> enumValueIntList = typeof(TESTENUM).GetEnumValues().Cast<object>().Select(m =>Convert.ToInt32(m)).ToList();if(!enumValueIntList.Exists(m => m == enumValueInt)){throw new Exception("cannot find type");}TESTENUM testEnumValueConvertInt;Enum.TryParse<TESTENUM>(enumValueString, out testEnumValueConvertInt);