Floating curly braces in C #

I came across a piece of C # code that I had not seen before. The programmer defined the block of code using only curly braces (not if, class, function, etc.).

{
    int i = 0;
}
i++; //compile error

Is there a purpose for this other than making the code more organized? Is it good, bad, or any practice to use these floating contexts?

+5
source share
6 answers

You can use an open and closed set of curly braces to define the block itself, which has its own region.

This is usually not considered good programming practice.

Usually, if someone does something like this, it's probably best to create a method / function in it.

+9
source

. , - , . , , i .

, , - , "" .

+1

{} # . , , , .

. , . , ?

+1

" " .

+1

, , int i , i .

0

. , .

, - , , , .

0

All Articles