Convert Generic List to Array

Here is a method to convert a generic list to an Array.

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

T[] theArray = new T[theList.Count];

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

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

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 ;
}
}