topcss / my-notes

https://github.com/topcss/my-notes/issues/new

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

c#的内部函数

topcss opened this issue · comments

commented

要实现函数内创建内部函数,至少有两种方式可以用。

第一种:Func<>
第二种:Action<>

Func和Action本质上都是委托,所不同的是Func可以输出返回值,而Action是没有返回值。下面给出实现的代码。

private void outputInfo(string info)
{
    Func<int, string, string> format = (count, message) =>
    {
        return message + " count:" + count.ToString();
    };

    Action<string> showMessage = (message) =>
    {
        Console.WriteLine(message);
    };

    string formatInfo = format(1, info);
    showMessage(formatInfo);
}

在outputInfo函数中又定义了两个函数format和showMessage。
其中format的前两个参数是输入参数,第三个参数是输出参数,即返回值。