ReDim for C#, using Generics
secretGeek .:dot Nuts about dot Net:.
home .: about .: sign up .: sitemap .: secretGeek RSS

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




name


website (optional)


enter the word:
 

comment (HTML not allowed)


All viewpoints welcome. But the right to delete any post for any reason is reserved. Don't make me do it. Comments may be republished, emailed to your loved ones or printed and used as toilet paper. Who reads this legal bit anyhow?

TimeSnapper is a life analysis system that stores and plays-back your computer use. It makes timesheet recording a breeze, helps you recover lost work and shows you how to sharpen your act.

TimeSnapper won last year's Developer Competition at Larkware.com, and is used by over 10,000 people.

Articles

Do they store the code for TFS in TFS? Do they store the code for TFS in TFS?
Sudden TimeSnapper Discount! Sudden TimeSnapper Discount!
How Can Microsoft Beat Google? How Can Microsoft Beat Google?
TimeSnapper 3.1: Attack of the the Red/Green Stripes TimeSnapper 3.1: Attack of the the Red/Green Stripes
21 tools used in our MicroISV 21 tools used in our MicroISV
Lost Treasures of the DOS World: tree! Lost Treasures of the DOS World: tree!
The Virtual Machine Machine and the Virtual Virtual Machine The Virtual Machine Machine and the Virtual Virtual Machine
Should Linq To Sql Go Should Linq To Sql Go "Open Source"?
Redux: New Synchronisation Idea Overlooked By Microsoft Redux: New Synchronisation Idea Overlooked By Microsoft
New Synchronisation Idea Overlooked By Microsoft Live team New Synchronisation Idea Overlooked By Microsoft Live team
Visual Studio UX Taskforce, Office UX Taskforce... etc. Visual Studio UX Taskforce, Office UX Taskforce... etc.
How to be Jeff Atwood How to be Jeff Atwood

Archives .: secretGeek :: Complete Archives :.
25 steps for building a Micro-ISV 25 steps for building a Micro-ISV
3 minute guides -- babysteps in new technologies: powershell, JSON, watir, F# 3 Minute Guide Series
Top 10 SecretGeek articles Top 10 SecretGeek articles

Downloads

TimeSnapper -- Automated Screenshot Journal TimeSnapper.com    
Version 3.1: instant productivity profiles

ShinyPower (help with Powershell) ShinyPower
Now at CodePlex

Next Action NextAction
Managing the top of your mind



[powered by Google] 


Thai Erawan, Brisbane Restaurant, delicious thai food in paddington Thai Erawan, Brisbane Restaurant
World's Simplest Code Generator (html edition) World's Simplest Code Generator
Gradient Maker -- a tool for making background images that blend from one colour to another. Forget photoshop, this is the bomb. Gradient Maker
How to be depressed How to be depressed
You are not inadequate.



Recommended Reading

The Best Software Writing I
The Business Of Software (Eric Sink)

Recommended blogs

Jeff Atwood
Reginald Braithwaite
Joseph Cooney
Phil Haack
Scott Hanselman
Julia Lerman
Joel Pobar
Eric Sink
Joel Spolsky
Des Traynor

Aggregated Links

programming.reddit.com
dzone
dot net kicks

Human Link Machines

interesting finds
a continuous learner's weblog
arjan's world
n links today
new and notable
morning coffee
learning .net
weekly link post
(my del.icio.us account)

LinkedIn profile
 
home .: about .: sign up .: sitemap .: secretGeek RSS .: © Leon Bambrick 2006 .: privacy

home .: about .: sign up .: sitemap .: RSS .: © Leon Bambrick 2006 .: privacy