8
public void Finalise()
    ProcessFinalisation(true);

Doesn't compile, but the correct version:

public void Finalise()
{
    ProcessFinalisation(true);
}

Compiles fine (of course).

If I am allowed if's without brackets when the following code has only one line:

if(true)
    CallMethod();

Why is the same not allowed for methods with one following line? Is there a technical reason?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • 1
    I believe ifs without brackets is advised against, but is there for historical reasons. I cannot seem to find my source at the moment, but maybe someone else can confirm this? – Bazzz May 16 '11 at 11:29
  • "if" is a statement and that rule is only for statements. Methods are the members of the classes. – Mert Susur May 16 '11 at 11:30
  • You would have to go back to the 1970's to find an answer. This comes from C. – H H May 16 '11 at 11:30
  • 1
    I think some of the grammar and key words are carried forward from C and C++. For example, I like keyword "me" over "this" :) – Sandeep G B May 16 '11 at 11:31
  • FYI a version of the feature you propose here is likely to make it into C# 6. See Roslyn.codeplex.com for details. – Eric Lippert Sep 22 '14 at 16:52

4 Answers4

14

The obvious answer is the language spec; for reasoning... I guess mainly simplicity - it just wasn't worth the overhead of sanity-checking the spec and compiler for the tiny tiny number of single-statement methods. In particular, I can potentially see issues with generic constraints, etc (i.e. where T : IBlah, new() on the end of the signature).

Note that not using the braces can sometimes lead to ambiguities, and in some places is frowned upon. I'm a bit more pragmatic than that personally, but each to their own.

It might also be of interest that C# inside razor does not allow usage without explicit braces. At all (i.e. even for if etc).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks marc, do you have an example where not using braces causes ambiguity? – Tom Gullen May 16 '11 at 11:32
  • 3
    @Tom - I mean *maintenance* ambiguity - the classic example being adding a second statement at the same indentation, not noticing the missing brace. – Marc Gravell May 16 '11 at 11:33
12

Marc is basically right. To expand on his answer a bit: there are a number of places where C# requires a braced block of statements rather than allowing a "naked" statement. They are:

  • the body of a method, constructor, destructor, property accessor, event accessor or indexer accessor.
  • the block of a try, catch, finally, checked, unchecked or unsafe region.
  • the block of a statement lambda or anonymous method
  • the block of an if or loop statement if the block directly contains a local variable declaration. (That is, "while (x != 10) int y = 123;" is illegal; you've got to brace the declaration.)

In each of these cases it would be possible to come up with an unambiguous grammar (or heuristics to disambiguate an ambiguous grammar) for the feature where a single unbraced statement is legal. But what would the point be? In each of those situations you are expecting to see multiple statements; single statements are the rare, unlikely case. It seems like it is not realy worth it to make the grammar unambiguous for these very unlikely cases.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • "the block of a statement lambda" - isn't that a bit tautological because a statement lambda is one with braces? – configurator May 19 '11 at 01:59
  • 1
    @configurator: Sure, but that's because we decided so. A statement lambda could be a lambda where any statement follows the => if we wanted. It's easier to say that it has to be a block. "action = x=>foreach(var y in x)Console.WriteLine(y);" could be a legal statement lambda if we had come up with rules to parse it. – Eric Lippert May 19 '11 at 06:37
2

Since C# 6.0 you can declare:

void WriteToConsole(string word) => Console.WriteLine(word)

And then call it as usual:

public static void Main()
{
    var word = "Hello World!";
    WriteToConsole(word);
}
iuliu.net
  • 6,666
  • 6
  • 46
  • 69
1

Short answer: C# is styled after C, and C mandates that functions be braced because of how C function declarations used to be.

Long version with history: Back in K&R C, functions were declared like this:

int function(arg1, arg2)
int arg1;
int arg2;
{ code }

Of course you couldn't have unbraced functions in that arrangement. ANSI C mandated the syntax we all know and love:

int function(int arg1, int arg2)
{ code }

but didn't allow unbraced functions, because they would cause havoc with older compilers that only knew K&R syntax [and support for K&R declarations was still required].

Time went on, and years later C# was designed around C [or C++, same difference in terms of syntax] and, since C didn't allow unbraced functions, neither did C#.