What is the corresponding branch? (suggested by ReSharper)

I use ReSharper to refactor my code, and one of ReSharper's suggestions is to use the appropriate branch instead of the if statement, which is always right.

If i have

if (myVar != null){ //code } 

And I apply the appropriate branch, the code is as follows

 { //code } 

Is this a brief if statement? What is it used for? What is the difference between brackets and nothing?

Thanks in advance.

+6
source share
2 answers

The block must remain there, or the semantics will change.

The amount of variables declared inside the branch block will change when curly braces are removed. Potentially with conflicting / hiding (lambdas) variables, this can lead to surprises.

To remove curly braces, place the cursor on one and press Alt-Enter, 'Remove Braces'

Re-insert curly braces: select a code block, Ctrl-Alt-J, 7 to surround with a block :)

+10
source

In ReSharper 6.0, this command also removes curly braces if you do not have variable declarations inside.

0
source

Source: https://habr.com/ru/post/891024/


All Articles