C# – Convert String Array to a List (.Net 2.0)

ASP.netSo you’ve got a C# string and you want to convert it to a List<string>.  Lists are one of the most efficient ways of storing…well…lists of things in .Net.  If you’re in .Net 3.0, it’s quite easy to go from a string array to a list by using the .ToList() function.

If you’re stuck in .Net 2.0 because your company won’t put 3.0 or higher on the servers, then going from a string (particularly one that is delimited with something you can split on) to a List is a little longer, but not much.

Here’s the code:


List<string> myVar = new List<string>(myString.Split('|'));

Note that the ‘|’ is the delimiter in your string that you can split on to get an array.  Simple enough.

Comments are closed.

Powered by WordPress. Designed by Woo Themes