none
lowerbound of array

    Question

  •  

    How can we set the lowerbound and upperbound of array .

     

    Ex: if i want ot set 0 to 6 to below array

     

    double[] sigmaChange,mueChange;

     

    Thanks,

    Vikas

     

    Thursday, May 22, 2008 7:42 AM

Answers

  • You can create .Net arrays with non-zero lower bounds, but they are a right pain to use:

    Array array = Array.CreateInstance(typeof(int), new int[] { 10 }, new int[] { 5 });
    Console.WriteLine("Lower bound = " + array.GetLowerBound(0));

    for (int i = 5; i < 15; ++i)
    {
        array.SetValue(i, i);
    }

    for (int i = 5; i < 15; ++i)
    {
        Console.WriteLine(array.GetValue(i));
    }

    Thursday, May 22, 2008 9:18 AM

All replies

  • All arrays in C# are zero-based. The first element is always at index 0 (zero). It's not possible to set a lower bound.
    Thursday, May 22, 2008 7:57 AM
  • that's right, you have to dimension the arrays like this:

     

    double[] sigmaChange = new double[7];

     

    that gives you 7 elements

     

    sigmaChange[0] through to sigmaChangeDevil

     

    .. that should be [ 6 ]!

    Thursday, May 22, 2008 9:10 AM
  • You can create .Net arrays with non-zero lower bounds, but they are a right pain to use:

    Array array = Array.CreateInstance(typeof(int), new int[] { 10 }, new int[] { 5 });
    Console.WriteLine("Lower bound = " + array.GetLowerBound(0));

    for (int i = 5; i < 15; ++i)
    {
        array.SetValue(i, i);
    }

    for (int i = 5; i < 15; ++i)
    {
        Console.WriteLine(array.GetValue(i));
    }

    Thursday, May 22, 2008 9:18 AM