0

I have java code where i can do this for inner loop's length

char[][] board = new char[3][3];

for(int a = 0; a < board.length; a++)
{
   for(int b = 0; b < board[a].length; b++)
   {
      //my code here
   }
}

but when i try it in c# as

for(int b = 0; b < board[a,].Length; b++)

it generates error

"Syntax error: value expected"

if i put the value and write it like

for(int b = 0; b < board[a,b].Length; b++)

it generates error

char does not contain a definition for Length

can someone help me out?

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[,] board = { { 'a', 'b', 'c' }, { 'd', 'e', 'f' }, { 'g', 'h', 'i' } };

            for (int a = 0; a < board.GetLength(0); a++)
            {
                for (int b = 0; b < board.GetLength(1); b++)
                {
                    char test = board[a,b];
                    //my code here
                }
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
Masu
  • 113
  • 1
  • 2
  • 12

1 Answers1

0

You can use the GetLength method. See here

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145