how to increase size of an array in C#.net

Y

yoshitha

Hi
Can any one tell me how to increase size of an array in C#.net with ASP.Net?

its very urgent for me
thanks
yoshitha
 
G

Guest

I use a function that expands an array by one element. You can modify it
slightly to suit your needs:

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

Kalpesh

Use ArrayList instead (growing array)
If you are using .net 2.0, use a generic version of ArrayList (this
will remove the need for casting for reference types and box-unbox for
value types (eg int)

Kalpesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top