I have an array of any type T
(T[]
) and I want to convert it into a List generic (List<T>
). Is there any other way apart from creating a Generic list, traversing the whole array and adding the element in the List?
Present Situation:
string[] strList = {"foo","bar","meh"};
List<string> listOfStr = new List<string>();
foreach(string s in strList)
{
listOfStr.Add(s);
}
My ideal situation:
string[] strList = {"foo","bar","meh"};
List<string> listOfStr = strList.ToList<string>();
Or:
string[] strList = {"foo","bar","meh"};
List<string> listOfStr = new List<string>(strList);
I am suggesting the last 2 method names as I think compiler or CLR
can perform some optimizations on the whole operations if It want inbuilt.
P.S.: I am not talking about the Array
or ArrayList
Type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With