When it comes to using
statements in C# (not to be confused with using
directives that import namespaces), Visual Studio doesn't indent single-line code that follows if no braces are employed. This is typical of "nesting" using statements as shown in this SO question.
I find it confusing that subsequent statements after using
are not indented, unlike the formatting of an if
statement:
// non-indented using statement
using (var myResource = new SomeIDisposableResource())
myResource.Indent(false);
// indented if statement
if (something == true)
IndentMe();
Is there any reason not to indent, or is it just preference?
// indented using statement, but not the default VS formatting
using (var myResource = new SomeIDisposableResource())
myResource.Indent();
EDIT:
Further testing reveals that I was incorrect about some of the VS formatting behavior. If you type a using statement:
using (var myResource = SomeIDisposableResource())
...and hit enter, the cursor will align with using
. If the next line is also a using statement, it will continue to align. If it is not, VS will indent it upon completion. Thus my original question is somewhat invalidated, because my first example is not really achievable unless you override the default formatting or use an IDE that doesn't do that.
Still, it is worth knowing that multiple using
statements are best treated as a single block because they technically are. The lack of indentation only applies when the statements are sequential using
statements without braces; and as one gets used to it, they stop looking so unusual.
As always thanks to all those who answered for the insight and experience in even these minor programming details.
Indentation improves the readability of the code. It is mainly used for code inside looping statements, control structures, functions etc. as good intended code is easy to maintain and is good looking. It makes the code more readable and easy to understand.
Select the lines you want to indent, and. use Ctrl + ] to indent them.
To automatically indent selected codeClick Format Selection in Edit, Advanced, or press CTRL+K, CTRL+F. Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.
You could also do the same but only to a selection of code by highlighting the block of code you want to realign, aligning it to the left side ( Shift + Tab ) and then after making sure you've selected the code you want to realign press Ctrl + K , Ctrl + F or just right click the highlighted code and select "Format ...
As others have said, always use braces. However, there's one idiom which somewhat goes against this and uses the "non-indentation":
using (Resource1 res1 = new Resource1())
using (Resource2 res2 = new Resource2())
using (Resource3 res3 = new Resource3())
{
// Do stuff with res1, res2 and res3
}
But I'd always use braces for the innermost block :)
It's preference. I always indent, and place the necessary items in brackets
using(var t = new t())
{
t.Foo();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With