tags:

views:

36397

answers:

9

Probably a really simple one this - I'm starting out with C# and need to add values to an array, for example:

int[] terms;

for(int runs = 0; runs < 400; runs++)
{
    terms[] = value;
}

For those who have used PHP, here's what I'm trying to do in C#:

$arr = array();
for ($i = 0; $i < 10; $i++) {
    $arr[] = $i;
}

Thanks, Ross

+5  A: 

You have to allocate the array first:

int [] terms = new int[400]; // allocate an array of 400 ints
for(int runs = 0; runs < terms.Length; runs++) // Use Length property rather than the 400 magic number again
{
    terms[runs] = value;
}
Motti
A: 
int[] terms = new int[400];

for(int runs = 0; runs < 400; runs++)
{
    terms[runs] = value;
}
John Nolan
+2  A: 
int ArraySize = 400;

int[] terms = new int[ArraySize];


for(int runs = 0; runs < ArraySize; runs++)
{

    terms[runs] = runs;

}

That would be how I'd code it.

JB King
+1  A: 

You can't just add an element to an array easily. You can set the element at a given position as fallen888 outlined, but I recommend to use a List<int> or Collection<int> instead, and use ToArray() if you need it converted into an array

Michael Stum
+33  A: 

You can either

int[] terms = new int[400];
for (int runs = 0; runs < 400; runs++)
{
    terms[runs] = value;
}

or, you can use Lists

List<int> list = new List<int>();
for (int runs = 0; runs < 400; runs++)
{
    list.Add(value);
}

// You can convert it back to an array if you would like to
int[] terms = list.ToArray();
DrJokepu
+2  A: 

c# arrays are fixed length and always indexed. Go with Motti's solution:

int [] terms = new int[400];
for(int runs = 0; runs < 400; runs++)
{
    terms[runs] = value;
}

note that this array is a dense array, a contiguous block of 400 bytes where you can drop things. If you want a dynamically sized array, use a List.

List<int> terms = new List<int>();
for(int runs = 0; runs < 400; runs ++)
{
    terms.Add(runs);
}

Neither int[] nor List is an associative array -- that would be a Dictionary<> in C#. both arrays and lists are dense.

Jimmy
+2  A: 

Answers on how to do it using an array are provided here.

However, C# has a very handy thing called System.Collections :)

Collections are fancy alternatives to using an array, though many of them use an array internally.

For example, C# has a collection called List that functions very similar to the PHP array.

using System.Collections.Generic;

// Create a List, and it can only contain integers.
List<int> list = new List<int>();

for (int i = 0; i < 400; i++)
{
   list.Add(i);
}
FlySwat
+25  A: 

If you're writing in C# 3, you can do it with a one-liner:

int[] terms = Enumerable.Range(0, 400).ToArray();

This code snippet assumes that you have a using directive for System.Linq at the top of your file.

On the other hand, if you're looking for something that can be dynamically resized, as it appears is the case for PHP (I've never actually learned it, then you may want to use a List instead of an int[]. Here's what that code would look like:

List<int> terms = Enumerable.Range(0, 400).ToList();

Note, however, that you cannot simply add a 401st element by setting terms[400] to a value. You'd instead need to call Add(), like this:

terms.Add(1337);
David Mitchell
A: 

Intialize the array first

int[] term = new int[initalizationvalue]

for retrieving

terms[position] = value;

for more information about arrays

http://csharp.net-informations.com/collection/csharp-array.htm

btn.

bolton