Hi all, just two quick questions to tax your brains with! I am preparing for a job interview and would like to get these two things clear!
Firstly, how can one find the upper limit of an array?
Second, how can one change that upper limit?
Thank you for your time.

10 Years
Discussion Span
Last Post by majestic0110

ok I think you can use the array datamember .length to find out the length of an array. Am I correct in thinking this ? Still need to know how to change the length of an array will update later.


>how can one find the upper limit of an array?
Assuming the upper limit in terms of capacity, you can use the Length property. my_array.Length gives you the number of items the array can hold, and my_array.Length - 1 gives you the last valid index.

>ow can one change that upper limit?
Rebind the reference to a new array:

using System;

namespace JSW {
  class Program {
    static void Main() {
      int[] my_array = new int[5];

      Console.WriteLine( "Size: {0} -- Last: {1}",
        my_array.Length, my_array.Length - 1 );

      my_array = new int[10];

      Console.WriteLine( "Size: {0} -- Last: {1}",
        my_array.Length, my_array.Length - 1 );

The size of an array is locked down when you create the object. If you want an array that can be resized, you use a List<T> object (or an ArrayList prior to .NET 2.0), or some other suitable collection.

Votes + Comments
Thank you, good explanation

well to get the upper or the lower limits of an array .net c# language library provides a function called GetUpperBound(x) and GetLowerBound(x)
x: is the dimension of the array
0 for 1-d
1 for 2-d

note:it follows 0th index notations


That's a good point shuban. For multidimensional arrays, the Length property will give you the total length of all dimensions. GetLowerBound and GetUpperBound are easier than calculating the length of each dimension manually. However, they're somewhat unconventional for arrays with a single dimension (the most common case) and jagged arrays.

This question has already been answered. 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.