Why do methods with only one statement need braces?

前端 未结 4 630
执念已碎
执念已碎 2020-12-15 12:17
public void Finalise()
    ProcessFinalisation(true);

Doesn\'t compile, but the correct version:

public void Finalise()
{
    Proce         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 12:23

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

提交回复
热议问题