Changing array length at runtime in C#.net

Dealing with arrays is a part of life for coders as they are almost every where in the code, so i thought that it would be the best topic to start my blog that deals with programming related topics.

Array Declaration in C#.net

DataType[] Array_Name=new string[Array_Length];

int[] Myarray=new int[10];

as we now know how to intialise an array, now lets store some values into it

Storing values in array

int Myarray = new int[10];
for (int i = 0; i < Myarray.Length; i++)
{
Myarray[i] = i;
}

The above code stores the values 1  to 10 in the array “Myarray”.

Ok enough of the basics, now lets go straight to Arrays with dynamic length

Creating arrays with dynamic length

  1. To start with declare an array of your choice with zero length (or any length), i am using an int array
  2. now when you are about to enter the values into increase the length of the array using Array.Resize method
  3. The syntax of the Array.Resize method is
  4. Array.Resize(ref Array_2_Resize, Length)
  5. Now pass the value to the Array as Myarray[Myarray.Length-1] = value;

Example: The following example demonstartes how to Increase the length of an array at runtime

int Myarray = new int[0];
for (int i = 0; i < 10; i++)
{
Array.Resize(ref  Myarray, Myarray.Length+1);
Myarray[Myarray.Length - 1] =  i;
}

simillarly you can use the same principle to decrease the size of an array at run time.

Please comment on this post, if you have any doubts.I will be very happy to help you

Update

Advertisement

5 Comments on “Changing array length at runtime in C#.net”

  1. simi says:

    hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com/2009/09/c-array.html
    http://www.csharptalk.com/2009/10/creating-arrays.html

    simi

  2. Safwan Ahmed says:

    Really super Article buddy.
    but u havent mention ur name and ur country
    I like arrays I wana playe the whole game with Arrays.
    If ur using arrays in ur code Superbly then let me have em all :)

    • chowdarysway says:

      Hi, safwan.ahmed My name is Vamsi Krishna and I’m from India.
      If i have any code worth sharing this is the place i post it, so check back again.
      Thank you for your comments, it keeps me going

  3. Harshvardhan says:

    Thanks………….
    I have used your coding and my problem has been solved.

    • Vamsi says:

      Thats great to know Harshvardhan


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.