0
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.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
stan
  • 81
  • 1
  • 12

2 Answers2

1

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)).

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
0

Use System.Linq's ToList method:

val_2 = val_1.ToList();

Make sure val_1 is initialized though!

Timmeh
  • 398
  • 2
  • 14