ReDim for C#, using Generics
As an exercise in using Generics, I wrote a ReDim function that works in C#. Bugs, feedback, criticism, welcome and appreciated. /// <summary> /// Redimensions the length of array down(or up) to equal "length" (parameter) /// This is based on the VB "redim" function, and like that one it does /// cause a memory overhead and a performance hit. So please use this one /// sparingly. For example, don't call it in a loop. /// (Hint: prefer List<T> with it's .Add() method, for frequently redim'd lists) /// </summary> /// <typeparam name="T">The Type of Array to be re-dimmed</typeparam> /// <param name="arr">The array to be re-dimmed</param> /// <param name="length">The new length of the array</param> private void ReDim<T>(ref T[] arr, int length) { T[] arrTemp = new T[length]; if (length > arr.Length) { Array.Copy(arr, 0, arrTemp, 0, arr.Length); arr = arrTemp; } else { Array.Copy(arr, 0, arrTemp, 0, length); arr = arrTemp; } }
'Wesner Moise' on Wed, 24 May 2006 01:59:24 GMT, sez: Try the existing framework method, Array.Resize
'lb' on Wed, 24 May 2006 02:03:35 GMT, sez: Now there's a very good reason why i didn't use Array.Resize.
An excellent reason in fact. A top one.
Let me see.
I don't like the name. Yes. "Resize". It's size-ist. And as a person of minimal stature (and decent girth), I simply hate size-ism.
And maybe i didn't notice that Array.Resize was there. That might also be possible.
Thanks Wesner ;-)
lb.
'lj' on Wed, 05 Jul 2006 12:04:25 GMT, sez: Array.Resize was only implemented in version 2 of the .NET framework so for anybody working with previous versions they may find this useful
lj
'lb' on Wed, 05 Jul 2006 20:32:27 GMT, sez: thanks lj!
>Array.Resize was only implemented in
>version 2 of the .NET framework
ahhh, i see.
>anybody working with previous versions
>[...] may find this useful
not as is... because generics also don't work in previous versions. although you could use the above code to write a non-generic version, for the specific case you need.
thanks again.
lb
'Paresh' on Mon, 30 Oct 2006 10:26:33 GMT, sez: you could use the above code to write a non-generic version,
'http://' on Thu, 30 Nov 2006 18:31:04 GMT, sez: Array.Resize doesn't handle multiple dimensions as does redim.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/5c12ce79-6616-4144-b3b6-4cffe3884dfd.asp
'Supun' on Tue, 27 Feb 2007 04:59:32 GMT, sez: hi, how to call this inside a method. i have a byte array and intiger as an argument. when applied the arry
if (length > arr.Length) { this willl throw an exception "Object reference not set to an instance of an object."
help me on this.
Supun
'lb' on Tue, 27 Feb 2007 05:32:36 GMT, sez: arr must be null (nothing)
'hajaworld' on Fri, 11 Jan 2008 02:26:39 GMT, sez: this is my sample code for single dimensional array
private void button5_Click(object sender, EventArgs e)
{
int[] intarr=new int[1];
for(int i=0,k=2;i<10;i++,k++)
{
intarr[i]=i;
intarr=this.returnarray(intarr,k);
}
}
private int[] returnarray(int[] old, int length)
{
int[] newarr = new int[length];
Array.Copy(old, newarr,old.Length);
return newarr;
}
|