How to increased array length dynamically?

public static Array ExpandArray(Array arr, object newElement)
{
int length = 1;
System.Type type = newElement.GetType();
if (arr != null)
{
length += arr.Length;
type = arr.GetType().GetElementType();
}
Array result = Array.CreateInstance(type, length);
if (arr != null)
{
arr.CopyTo(result, 0);
}
result.SetValue(newElement, result.Length - 1);
return result;
}

Tagged , . Bookmark the permalink.

Leave a Reply