Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey, i know that C# inits the array to the default(T) when is first initialized.
(int[] ar = new int[n]);

is there any way to initialize the array:
a[0] = 0
a[1] = 1
...
a[n] = n

without any loops or recursion?

thanks

by the way, my solution was similar to this:


public class intArray
{
  int[] arr;
  bool[] gotArray;

  public intArray(int size)
  {
    arr = new int[size];
    gotArray = new bool[size];
  }

  // dont remember exactly the [] syntax but:
  public int operator [](int index)
  {
     get
     {
        if (!gotArray[index])
           return index;
        else
           return arr[index];
     }
     set
     {
        arr[index] = value;
        gotArray[index] = true;
     }
     
  }

}


but it seems that there's a better solution
Posted
Updated 11-Aug-11 7:00am
v2

You could cut out the bool array in your original by using a nullable int

C#
public class intArray
    {
          int?[] arr;
           
          public intArray(int size)
          {
                arr = new int?[size];
          }

          public int this[int index]
          {
                get
                {
                    if (arr[index] == null)
                        arr[index] = index;
                    
                    return (int)arr[index];
                }
                set
                {
                    arr[index] = value;
                }
          }
    }


    class Program
    {
        static void Main(string[] arguments)
        {
            intArray iarr = new intArray(10);

            for (int i = 0; i < 10; ++i)
                Console.Out.WriteLine(iarr[i]);

            Console.ReadKey();
        }
    }
 
Share this answer
 
Comments
arielb 11-Aug-11 13:28pm    
similar idea, but if there's no better solution i'll take that.
thanks!
BillWoodruff 11-Aug-11 20:59pm    
While I cannot imagine a 'real-world' use for an array's values to be initialized to a sequence ... since an array essentially embodies a sequence ... I like the 'out-of-the-box' thinking in LeWax00's proposed solution, and can see real value in the 'real world' in using an array that can embody 'undefined' as well as sequence. Good one, LeWax00, got my +5. Arrays of #n dimensions, even if 'ragged,' would never require recursion in my experience, and 'for loops are fast.
You mean something like:
int[] ar = new int[3] { 0, 1, 2 };
 
Share this answer
 
Comments
arielb 11-Aug-11 12:55pm    
nope, because u dont know whats the number of n, its variant.
if it would be constant then this initiazlization would be ok.

i got a tip to think more object oriented style.
Wendelius 11-Aug-11 13:17pm    
You don't have to define the length so you can also use:
int[] ar = new int[] { 0, 1, 2 }
But this isn't what you're after?
arielb 11-Aug-11 13:24pm    
no, you don't know the length of the array, u are getting that as a parameter.
means, what u have in ur hand is
int[] array = new int[n]

now, initialize it without a loop
C#
int[] ar = new int[3];
ar.Initialize();
 
Share this answer
 
Comments
arielb 11-Aug-11 13:26pm    
no.. it inits everything to 0... not 0,1,2,3.. etc

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900