C#: Function in Function possible?

前端 未结 7 984
面向向阳花
面向向阳花 2020-12-05 05:59

Is it possible to declare a method within another method in C#?

For example like that:

void OuterMethod()
{
    int anything = 1;
    InnerMethod();          


        
7条回答
  •  青春惊慌失措
    2020-12-05 06:45

    The delegate keyword does just that.

    void OuterMethod()
    {
        var InnerMethod = delegate()
        {
            int PlitschPlatsch = 2;
        };
    
        int anything = 1;
        InnerMethod();
    }
    

    Don't forget that, per scoping, the PlitschPlatsch variable won't be accessible outside InnerMethod.

提交回复
热议问题