C#:函数中的函数可能吗? [英] C#: Function in Function possible?

查看:16
本文介绍了C#:函数中的函数可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 C# 中的另一个方法中声明一个方法?

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

例如:

void OuterMethod()
{
    int anything = 1;
    InnerMethod();     // call function
    void InnerMethod()
    {
        int PlitschPlatsch = 2;
    }
}

推荐答案

更新:本地函数 在版本 7 C# 中添加.

Update: Local functions where added in version 7 C#.

void OuterMethod()
{
    int foo = 1;
    InnerMethod();
    void InnerMethod()
    {
        int bar = 2;
        foo += bar
    }
}

在以前的 C# 版本中,您必须使用这样的操作:

In previous version C# you have to use action like this:

void OuterMethod()
{
    int anything = 1;
    Action InnedMethod = () =>
    {
        int PlitschPlatsch = 2;
    };
    InnedMethod();
}

这篇关于C#:函数中的函数可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆