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
                }
            }
        }
    }
}
3
  • Is you c# array in the format board[][] or board[,]? Commented Jan 21, 2016 at 13:27
  • @jdweng its char [,] board = board [3,3] Commented Jan 21, 2016 at 13:29
  • I added the solution to your posting since I couldn't create new answer since question been marked duplicate. Commented Jan 21, 2016 at 14:02

1 Answer 1

0

You can use the GetLength method. See here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.