2

Hi i am moving to c# now from VBS and .net this is my first attempt at making anything in C# so it is a very basic question. How do i make an array?

Below is my current attempt

        string[] arr1 = new string[1, 2];
        arr1[0, 0] = "One";
        arr1[0, 1] = "two";
        arr1[0, 2] = "three";
        arr1[1, 0] = "four";
        arr1[1, 1] = "five";
        arr1[1, 2] = "six";
Kieranmv95
  • 828
  • 4
  • 14
  • 31
  • 1
    Use `string[,] arr1` instead of `string[] arr1`. – Alessandro D'Andria Mar 19 '14 at 11:19
  • 1
    `string[,] arr1 = new string[2, 3];` Note comma on left side and dimentions on the right – Andrei Mar 19 '14 at 11:20
  • 1
    For this example, the declaration should be `string[,] arr1 = new string[1,2];` : the comma must appear both in the declaration and in the definition of the array. Also, the numbers inside the squared brackets mean the amount of element. In your example, you only have one element of each two elements in your array : `arr1[0,0]="One"; arr1[0,1]="Two"` – Alex Barac Mar 19 '14 at 11:21
  • http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx – Incredible Mar 19 '14 at 11:27
  • adding the comma in the declaration worked great, Thanks – Kieranmv95 Mar 19 '14 at 11:29
  • @Tyagi i know about MSDN first place i went as i used to do VBS but it can sometimes make things more confusing :( – Kieranmv95 Mar 19 '14 at 11:32

3 Answers3

2

There is a site named MSDN :D , You can find all basic syntax related stuffs with detailed examples there :D , http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx

Amarnath R Shenoy
  • 5,121
  • 8
  • 24
  • 32
1

The array you declared has one row and two columns and you are trying to access two rows and three columns. With your code you will get exception. You also need a comma for two dimension on left side. You can read more about using multi-dimension here.

string[,] arr1 = new string[1, 2];
arr1[0, 0] = "One";
arr1[0, 1] = "two";
Adil
  • 146,340
  • 25
  • 209
  • 204
  • but i thought arrays where zero based so this was possible? – Kieranmv95 Mar 19 '14 at 11:22
  • 1
    @Kieranmv95: Yes, they're zero-based - but the `new string[1, 2]` gives the *length* of each dimension, not an inclusive upper bound. – Jon Skeet Mar 19 '14 at 11:24
  • It is zero based, if you give the size 1 then it will have only one element at index zero. In you case first dimension is 1 so one row and second dimension is 2 so two columns – Adil Mar 19 '14 at 11:28
1

Look at your declaration: string[] arr1 = new string[1, 2]; You are trying to asign a 2 dimensional array to a one dimensional array. The proper declaration would be:
string[,] arr1 = new string[1, 2]; //notice the , in the declaration

Furthermore, you have defined your first dimension with a length of 1 and your second with a length of 2, while in your code:

arr1[1, 0] = "four";
arr1[1, 1] = "five";
arr1[1, 2] = "six";

You are treating it as if your first dimension had a length of 2 and your second as if it had a length of 3. This will throw an exception.

Mario Stoilov
  • 3,411
  • 5
  • 31
  • 51