7

Possible Duplicate:
When do you use code blocks?

Ok, this might be a stupid question and I might be missing something obvious but as I slowly learn C# this has kept nagging me for a while now.

The following code obviously compiles just fine:

public void Foo()
{
    {
        int i = 1;
        i++;
    }

    {
        int i = 1;
        i--;
    }
}

I understand that {} blocks can be used for scoping. The question is why would you want to do this? What problems does this feature solve?

There is no harm that I can see in using them barring that it does add to a more confusing code as these kind of scopes can be more easily overlooked compared those "tied" to flow controls, iterations, etc.

Community
  • 1
  • 1
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • case statements is where its really useful, so you don't get errors for reusing something like `i` in a loop. – Chad Jan 11 '12 at 18:03

4 Answers4

6

It is useful in switch statements where your variable declarations can become confusing:

Confusing code:

switch (true)
{
    case true:
        var msg = "This is true.";
        var copy = msg;
        break;

    case false:
        msg = "This is false.";
        copy = msg;
        break;
}

Clear code:

switch (true)
{
    case true:
    {
        var msg = "This is true.";
        var copy = msg;
        break;
    }

    case false:
    {
        var msg = "This is false.";
        var copy = msg;
        break;
    }
}
Devin Burke
  • 13,642
  • 12
  • 55
  • 82
  • Most fun and stupid when people uses additional line breaks, etc. instead of just bracers/blocks – abatishchev Jan 11 '12 at 18:05
  • 1
    Thanks! Lol I've been using them then as I always write `switch` statements the second way. The funny and sad part about it is I thought I was actually using "case blocks" not anonymous blocks...got a lot to learn still :(... – InBetween Jan 11 '12 at 18:09
3

I would say that creating an artificial block for purposes of scoping is often simply confusing, as you note. I've never found a need for it.

If there was some perceived need for it, my guess is refactoring into a separate method is usually a better option.

(note I'm not saying it's absolutely bad. I just think you'd need some really specialized need to have it)

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
1

For 'bare' scoping blocks like these I'd tend to agree with you. From a language design perspective I think that they are a generalization of the implicit scoping blocks in for, while, and if conditions, where they are more important. For example:

foreach (var x in foos) {
    var someMessage = x.ToString();
    doSomething(someMessage);
}
Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • I agree completely. This begs the question why it is allowed in the first place...there must be some advantages to this feature. – InBetween Jan 11 '12 at 18:01
  • @InBetween, conversely, why wouldn't it be allowed? The language allows for `foreach (var x in foos) statement`, where `statement` is a legal statement in code. What's the statement here? A `{ codeblock; }`. We have just established that `{ codeblock; }` is a legal statement. So the question might not then be why allow it, but what is the compelling reason to disallow it. But I haven't bumped my comment here up against the language specification or other resources, so maybe consider this just a bunch of words. – Anthony Pegram Jan 11 '12 at 18:04
0

The quick answer is it keeps you from inadvertently changing a variable in an unexpected way. By having it "in scope" you can act on it only when you want to and not "accidentally" change the value

Brian
  • 2,229
  • 17
  • 24