C# Empty Statement

后端 未结 13 2252
情深已故
情深已故 2020-11-29 12:34

The C# language specification defines the empty-statement grammar production, which allows me to do something like this:

static void Main(string[] a         


        
相关标签:
13条回答
  • 2020-11-29 13:03

    Imagine you have some code that you want to extend by a new feature:

    void DoSomething()
    {
        // ...
        NewMethod();
    }
    

    Now you introduce a new member called NewMethod. However you don´t have the time to implement it appropriately and leave it empty:

    public void NewMethod() { }
    

    Some time later you can finally implement the code of that member without having to bother where and how to call it, because that was already done before.

    You can also create an interface which you don´t implement. This way you only define what to do, but not how.

    Another possible scenario is to have a virtual member that usually does nothing, but in a few cases (some specific implementation) you want the method to return a specific value. See this post for example: Virtual methods without body

    0 讨论(0)
提交回复
热议问题