开发者

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.


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();
}


Easy fix: always use explicit blocks, even for single lines. Visual Studio will then properly indent, and as a bonus your code will be more maintainable!


Like my C instructor told me over 10 years ago: Always, always, always use the braces. Odds are good someone is going to come along (possibly even you) and throw in another line of code, then wonder why it's not behaving correctly.


I like being downvoted when I'm wrong, so I'm going to make an answer out of this...

Here's how I would format it:

using (Resource1 res1 = new Resource1())
using (Resource2 res2 = new Resource2())
using (Resource3 res3 = new Resource3())
  DoStuffWithResources(res1, res2, res3);

If I were to replace DoStuffWithResources with multiple statements, I would make use braces. However, my editor prevents me from ever making the following mistake:

using (Resource1 res1 = new Resource1())
using (Resource2 res2 = new Resource2())
using (Resource3 res3 = new Resource3())
  DoStuffWithResources(res1, res2, res3);
  DoOtherStuffWithResources(res1, res2, res3);

When I try to enter the above, I immediately get:

using (Resource1 res1 = new Resource1())
using (Resource2 res2 = new Resource2())
using (Resource3 res3 = new Resource3())
  DoStuffWithResources(res1, res2, res3);
DoOtherStuffWithResources(res1, res2, res3);

Pro tip: A downvote is not a counterargument, so if you don't have one, you shouldn't be voting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜