Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

I found in legacy code following line:

protected bool[,] PixelsChecked;

What does [,] mean here?

like image 995
Valentin H Avatar asked May 19 '13 12:05

Valentin H


People also ask

What does bool mean in C?

In C, Boolean is a data type that contains two types of values, i.e., 0 and 1. Basically, the bool type value represents two types of behavior, either true or false. Here, '0' represents false value, while '1' represents true value. In C Boolean, '0' is stored as 0, and another integer is stored as 1.

Why do we use bool in C?

In C the terminology of boolean is associated with data type and Boolean is termed as one of the data types in the C Standard Library. This data type stores one of the two possible values denoted by true or false, which represents two truth values of logic and Boolean Algebra.

Where is bool defined C?

bool exists in the current C - C99, but not in C89/90. In C99 the native type is actually called _Bool , while bool is a standard library macro defined in stdbool. h (which expectedly resolves to _Bool ). Objects of type _Bool hold either 0 or 1, while true and false are also macros from stdbool.

Is there bool type in C?

The C99 standard for C language supports bool variables. Unlike C++, where no header file is needed to use bool, a header file “stdbool. h” must be included to use bool in C.


2 Answers

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.

like image 161
Lasse V. Karlsen Avatar answered Oct 01 '22 09:10

Lasse V. Karlsen


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];
like image 22
pascalhein Avatar answered Oct 01 '22 08:10

pascalhein