string[] val_1;
List<string> val_2;
I'm trying to convert string
array to string
list. I need an effective way.
Both should be in string
list format to compare both.
string[] val_1;
List<string> val_2;
I'm trying to convert string
array to string
list. I need an effective way.
Both should be in string
list format to compare both.
There are two options.
Using LINQ ToList()
extension method - add using System.Linq;
to the top of your source file and then do:
var list = array.ToList();
Second option is to directly initialize the list via the constructor:
var list = new List<string>(array);
There is no difference in performance between these two approaches, both will take linear time relative to the number of items in the array (O(N)
).
Use System.Linq
's ToList method:
val_2 = val_1.ToList();
Make sure val_1
is initialized though!