Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting/indentation for using statements (C#)

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.

like image 812
JYelton Avatar asked Sep 14 '10 17:09

JYelton


People also ask

Do indentations matter in C?

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.

How do you indent a section of code?

Select the lines you want to indent, and. use Ctrl + ] to indent them.

How do you indent in C sharp?

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.

How do you fix indents in C#?

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 ...


2 Answers

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 :)

like image 124
Jon Skeet Avatar answered Sep 20 '22 21:09

Jon Skeet


It's preference. I always indent, and place the necessary items in brackets

using(var t = new t())
{
   t.Foo();
}
like image 26
kemiller2002 Avatar answered Sep 19 '22 21:09

kemiller2002