1

I'm very, very new to C# and would like to ask a maybe very stupid question, the first language I learned was Java, in which I could do this:

int[][] array = new int[1600][900];
array[600][400] = 10;

for(int x = 0; x < 1600; x++)
{
    for(int y = 0; y < 900; y++)
    {
        int something = colour[x][y];
    }
}

Now I've searched the web for quite a while, but I've got no idea about how to do this in C#

EDIT:

Thanks for the help everyone, it's been usefull :)

1

4 Answers 4

4

Just use a comma :

int[,] array = new int[1600,900];
array[600,400] = 10;
//...
Sign up to request clarification or add additional context in comments.

1 Comment

Why don't you want to use implicit typing?
2

You can do it in a very similar way in C#:

int[,] array = new int[1600,900];
array[600,400] = 10;

for(int x = 0; x < 1600; x++)
{
    for(int y = 0; y < 900; y++)
    {
        int something = colour[x,y];
    }
}

I'm not sure if I understand what's the purpose of the code in the double for cycle. I suppose those three pieces of code don't have anything in common.

2 Comments

It's basicly for making a z-buffer, which contains the depth of all the pixels of my screen.
Of course, I understand :)) I was looking for a connection between those three pieces of code.
0
int [,] array = new int[1600,900];

Comments

0

To add some color to the answers: In .NET, an int[][] is a jagged array, or an array of arrays. While this may be a perfectly good structure for you to use, it has the addded overhead that each array must be initialized individually. So your initialization would be:

int[][] array = new int[1600][];
for(int i=0;i<array.Length;i++)
    array[i] = new int[900];

now you can access an individual value by using

array[600][400] = 10;

One benefit of using jagged arrays is that the "interior" array can be different sizes. If you don;t need that flexibility than using a rectangular ([,]) array may be a better option for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.