Tuesday, August 3, 2010

Converting array of type T to generic List<T>

This requires a very simple code with just the initializer of the generic list but yet when I googled it I didn't get much out of them on the first search result page.

For our case lets consider the string type. The following code exemplifies how an array of string can be converted to a generic list of string type.

string[] arrayOfString = new string[] { "Just", "an", "example."};
List listOfString = new List(arrayOfString);

And yes there you have it! Its just that simple.

No comments:

Post a Comment