Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 

YOU ARE HERE: HOME Questions How can I create an non zero base indexed array



Create an non zero base indexed array

View(s): 15942

How can I create an non zero base indexed array?

Answer 1)


Yes, It's possible to create the arrays that have non-zero based index.

You can dynamically create your own arrays by calling Array's static CreateInstance method. There are several overloads of this method, but they all allow you to specify the type of the elements in the array, the number of dimensions in the array, the lower bounds of each dimension, and the number of elements in each dimension.

But keep in mind that nonzero-based arrays are not CLS (Common Language Specification)-compliant.

The following code demonstrates how to dynamically create a two-dimensional array of System.Decimal values.


public sealed class Program
{
    public static void Main()
    {
        // We want a 2-dim array [2001..2010][1..4]
        Int32[] lowerBounds = { 2001, 1 };
        Int32[] lengths = { 10, 4 };
        Decimal[,] quarterlyIncome =
            (Decimal[,])Array.CreateInstance(typeof(Decimal), lengths, lowerBounds);      

        Console.WriteLine("{0,4}  {1, 9}  {2, 9} {3, 9}, {4, 9}",
                      "Year", "Q1", "Q2", "Q3", "Q4");       

        Int32 firstYear = quarterlyIncome.GetLowerBound(0);
        Int32 lastYear = quarterlyIncome.GetUpperBound(0);
        Int32 firstQuarter = quarterlyIncome.GetLowerBound(1);
        Int32 lastQuarter = quarterlyIncome.GetUpperBound(1);

        for (Int32 year = firstYear; year <= lastYear; year++)
        {
            Console.Write(year + " ");
            for (Int32 quarter = firstQuarter; quarter <= lastQuarter; quarter++)
            {
                Console.Write("{0,9:c}  ", quarterlyIncome[year, quarter]);
            }
            Console.WriteLine();
        }
    }
}      

The first dimension represents calendar years and goes from 2001 to 2010, inclusive. The second dimension represents quarters and goes from 1 to 4, inclusive.


Output:

  Asked in:  TCS (Tata Consultancy Services)   Expertise Level:  Experienced
  Last updated on Thursday, 19 January 2012
4/5 stars (3 vote(s))

Register Login Ask Us Write to Us Help