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.

4
  • 4
    val_2 = val_1.ToList() Commented Nov 26, 2018 at 14:18
  • 1
    "Both should be in string List format to compare both" Why you can't compare a list with an array? For example: val_1.SequenceEqual(val_2). Commented Nov 26, 2018 at 14:21
  • @PrasadTelkikar that specific question is about an Array object, but it still definitely is a duplicate. Commented Nov 26, 2018 at 14:27
  • @KlausGütter Thank you it worked Commented Nov 27, 2018 at 6:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Use System.Linq's ToList method:

val_2 = val_1.ToList();

Make sure val_1 is initialized though!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.