14

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.

13
  • I don't get it. Can you show examples of both styles? The question you linked to shows indented code... with curlies... Commented Sep 14, 2010 at 17:32
  • @Martinho Fernandes: Added examples as requested. Commented Sep 14, 2010 at 17:35
  • 1
    Works fine on my machine, it indents as soon as I type ;. I did override the default settings though. Commented Sep 14, 2010 at 18:32
  • It indents by default. Even if you carefully remove the indentation, clicking Format Document restores it. Commented Sep 14, 2010 at 18:36
  • If you insist on skipping the braces I would stick the using statement and the myResource.Indent() call on the same line. Though I prefer braces in this case. I don't particularly mind if (something == true) IndentMe(); as a one-liner, though. Commented Sep 14, 2010 at 19:07

5 Answers 5

27

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

Sign up to request clarification or add additional context in comments.

9 Comments

Good call, I forgot about multiple using statements on top of each other.
The stack of using statements seems to go against my model of code dependencies. By that, I mean that I view indented code as being dependent on the preceding block with one less indent. Inserting some code between the using statements would seem okay based solely on formatting, but unless I am wrong, it would break the association between the statements. I guess this is just one of the occasional exceptions to the rule?
If you're inserting code between two lines, presumably you're looking at those lines.
@JYelton: Yes, it's an exception - I rarely find myself doing it anyway, to be honest, but it's useful to know about. Basically I mentally treat all the using statements as a block of them.
If we're going to allow stacking, then I'm not sure why we need the braces.
|
13

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

using(var t = new t()) { t.Foo(); } 

10 Comments

Even for single lines? I presume the braces then just force VS to indent according to your preference?
Yes, I even put it for single lines. It's preference, but I have run into too many situations where someone accidentally comments or deletes something and an if statement extends it's control beyond where it should. Sometimes its a massive headache that causes hours of debugging. I follow the explicitly clear and better safe than sorry principle.
Kevin, were any of these situations in C# as opposed to C++ or C?
@JYelton: Keep in mind that VS will indent regardless of the braces.
@Steven, actually yes. When I have had this happen in C# it is generally, because the code is bad to begin with. It's confusing, and sometimes poorly done. For a long time, I never thought this would make that much of a difference in a language like C# (compared to C++, C) it is a lot easier to understand. And then i saw the C# code where it happened. I was unbelievably frustrated when I figured out that was the problem.
|
5

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!

4 Comments

Do you mean more maintainable because braces affect variable scope?
@JYelton, not quite. More maintainable because if you later want to add a line there or remove it for debugging or something, the braces are already there, and you don't risk forgetting them and wasting hours debugging it.
@Martinho Fernandes: I've had such situations occur; definitely a good reason to always use braces.
It's impossible to forget them unless you're using Notepad or something. The VS editor prevents this error.
0

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.

1 Comment

Really? It's served me well since c# first came out. Why would you say it doesn't apply?
0

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.

7 Comments

My argument for using braces isn't because of what I might do, it is the next person that comes in that I do not trust. Much like driving on a snowy road in winter - I know that I can take care of myself, it is everyone else driving that scares the living hell out of me. Point given, you will recognize that the second isn't indented - but how about the Jr. that gets brought in to do defect work?
The next person is also using the VS editor, so the same thing prevents them from accidentally creating that second code block. If you're in a typical shop, code will be reviewed prior to being released or put into production. As part of code review, I always Format Document to ensure that nothing changes.
@Steven, code will be reviewed? I wish! If only the LOB saw such matters as clearly as programmers do.
@Anthony: It's actually fine if you downvoted -- you're allowed to be wrong. All I ask is that people offer a counterargument as opposed to a thumb. Your argument seems to be that some shops don't do code reviews. Sadly, you're right that these exist. However, you're also right in your implication that they suffer for this. Not much point talking about the benefits of one formatting over another if, in the end, you can get away with murder.
It turned out that I did downvote but didn't mean to. But on code reviews, I'm doing work on a team for a rather large financial company. The LOB is paying for enhancements. We (meaning the grunts) all agree that code reviews would be nice. Refactoring obscenely long methods would be wonderful. (And I do mean obscenely long.) It would be so much better for the LOB in the long run. They don't care about the long run, they care about the next release. But don't mind me, I'm basically just venting now.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.