C# Arrays

dataflame

Beta member
Messages
1
Location
Earth
Hi, I'm fairly new to C# and I am wondering if there is an easier way to do the following:

Code:
int[] Ages; = {};

I want to have 1 to 100 inside of this array, but I'm not sure if there is an easier or quicker way to do that.

Sorry if this is a stupid question, but any help is appreciated.
 
Using a for loop:
Code:
var ages = new int[100];
for(i = 0; i < 100; i++)
{
    ages[i] = i + 1;
}

Or with LINQ (slightly slower):

Code:
var ages = Enumerable.Range(1, 100).ToArray();

If you plan on modifying the list at all, then I would suggest using a List<int> instead of an array.
 
Hi, I'm fairly new to C# and I am wondering if there is an easier way to do the following:

Code:
int[] Ages[B];[/B] = {};

I want to have 1 to 100 inside of this array, but I'm not sure if there is an easier or quicker way to do that.

Sorry if this is a stupid question, but any help is appreciated.

I do believe that semi colon shouldn't be there :p Syntax error my friend :p (Note bolded semi colon, kinda hard to see)
 
Back
Top Bottom