Friday, November 15, 2013

[C#] How to convert StringCollection to List<String> or other IEnumerable<String> type

Starting from .NET 3.5, converting StringCollection to List<String> or other IEnumerable<String> type became easy.

Just use powerful Enumerable.Cast<TResult> method from System.Linq namespace:

StringCollection stringCollection = new StringCollection();
...
List<String> stringList = stringCollection.Cast<String>().ToList();

Full example on GitHub: StringCollection2IEnumerableString.cs

1 comment: