0

Hi folks my question is how can I determine the size of an array in runtime dynamically. Like in VB we do this :-

Dim x() as Integer,i as Integer

i=5
Redim Preserve x(i)

so what is the equivalent syntax for this in C#?

3
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Ramy Mahrous
0

Another approach is to use Generics.
List<int> x = new List<int>();

x.Add(0); x.Add(1); ..... as many as you want, as often as you want.
When/If you ever need to use it as an array, you can always do this:

int[] z = x.ToArray();


The advantage here is that you can use the List for anything you can do with an array, only you have the added benefit of removing items, and adding items at will without dealing with redimensioning issues like copying the data from the old array into the newly sized array.

IMHO, Lists are far superior to the old style array.

// Jerry

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.