Convert IEnumerable to <string> in .net 2.0

I need to convert IEnumerable to List<string> . One way is to add each object to the list through this, but this process takes a lot of time. Is there any other way to do this. Any help would be appreciated.

+4
source share
1 answer

You can pass the enumerated directly to the constructor of the new List<string> instace:

 List<string> list = new List<string>(yourEnumerable); 

or you can use the AddRange method:

 List<string> list = new List<string>(); list.AddRange(yourEnumerable); 

This answer assumes that you actually already have an IEnumerable<string> .
If not, the only option is a loop:

 List<string> list = new List<string>(); foreach(object item in yourEnumerable) list.Add(item.ToString()); 

By the way: you say:

one way is to use this to add each object to the list, but this process depends on the time

You need to list the enumerated value anyway. How else do you get all the values? In the first two code examples I gave you, the listing is done inside the List<T> class, but it is still happening.

+13
source

Source: https://habr.com/ru/post/1438001/


All Articles