简体   繁体   中英

bool [,] - What does this syntax mean in c#?

I found in legacy code following line:

protected bool[,] PixelsChecked;

What does [,] mean here?

It's a two-dimensional array.

In .NET you can have two types of arrays that aren't single dimension:

  1. Multidimensional arrays:

     int[,] a; // 2 dimensions int[,,] b; // 3 dimensions, and so on 
  2. Jagged arrays (arrays of arrays):

     int[][] a; // an array of arrays of ints int[][][] a; // an array of arrays of arrays of ints 

In both cases you need to initialize the variable before using it though.

Usage is also different, in the first case:

int value = a[1, 2]; // note the comma, and single pair of brackets

In the second case, you need to address each array separately:

int value = a[1][2]; // the first [1] will return an array, and then you take
                     // the 3rd element (0-based) of that

Also remember that you can initialize a multidimensional array in just one statement:

int[,] a = new int[10, 20];

whereas a single statement for a jagged array will create a single array full of null-references:

int[][] a = new int[10][];

You will also need to initialize all the elements of that array to their corresponding array references, here's a quick way to do that with LINQ in one statement:

int[][] a = Enumerable.Range(0, 10).Select(new int[20]).ToArray();
// 10 x 20

Also see the MSDN Page on the subject for more information.


Fun fact: The JITter produces faster code for accessing jagged arrays than it does for multidimensional arrays, see this question for more information.

The [,] is a 2-dimensional array.

You can initialize it like this:

protected bool[,] PixelsChecked = new bool[Width, Height];

This is how you access it:

bool leftTop = PixelsChecked[0, 0];

It is basically a rectangle with values, and you can access them with [x,y] .

You could also create 3- and more-dimensional arrays with

protected bool[,,] Cube = new bool[5,5,5];

protected bool[,,,] _4dimensional = new bool[10,10,10,10];

It is the syntax of Multidimensional Arrays

Arrays can have more than one dimension.

In this case, it is two-dimensional array. An array can have many dimensions. Multidimensional arrays are available using a special syntax in C#.

For syntax usage, if you want to declare n-diamensional array , you shoud use comma n-1 times.

When you use bool[,] PixelsChecked it is declaring two-dimensional array called PixelsChecked and their elements typed as boolean .

Example;

bool[,] PixelsChecked = new bool[2,2];

PixelsChecked[0, 0] = true;
PixelsChecked[0, 1] = false;
PixelsChecked[1, 0] = true;
PixelsChecked[1, 1] = false;

Remember, you need to initialize the array before using it.

For example , two-dimensional arrays model a plane while three-dimensional arrays model a cube or other structure.

It is a two dimensional array , basicly a 2D grid of boolean values.

To create one you can do this

protected bool[,] PixelsChecked = new bool[Width, Height];

You could even make a 3rd dimension

protected bool[,,] PixelsChecked = new bool[Width, Height, Length];

If you want to get or set a certain coordinate

bool Value  = PixelsChecked[X,Y];

You can do alot with arrays, and there are many types. You can find a nice tutorial here and here .

bool[,] is a two dimensional array of bool s.

Basically, instead of having the array like this:

true, false, false, true, false, true, etc.

It's kind of structured like this:

true, false, false, true, false, true, etc.
true, false, false, true, false, true, etc.
true, false, false, true, false, true, etc.
true, false, false, true, false, true, etc.
true, false, false, true, false, true, etc.
true, false, false, true, false, true, etc.
etc.

And so you can access, let's say the second item down and the third one right, like this:

arrayName[1,2];

There's other ways to do this, like to make an array of arrays, but this keeps a constant length, and is better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM