Convert an Array to a generic list

Sometimes you have an array, but a generic list would make better sense.
Here is a method to convert from an array to a generic list.

This comes in handy if you you get an array back a web service (since they can’t return a generic list).

public static List<T> ConvertArrayToList<T>(T[] theArray)
{
try
{
if (theArray == null)
{
return null;
}

List<T> theList = new List<T>();

for (int i = 0; i < theArray.Length; i++)
{
theList.Add(theArray[i]);
}

return theList;
}
catch (Exception ex)
{
throw ;
}
}

Advertisements

Got something to add?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s