相关疑难解决方法(0)

179
推荐指数
2
解决办法
7万
查看次数

C#Variable = new function(){};

在C#中,是否可以动态创建新函数来定义变量?

我知道

string getResult() {
    if (a)
        return "a";
    return "b";
}
String result = getResult();
Run Code Online (Sandbox Code Playgroud)

是可能的,但我正在寻找类似的东西

String result = new string getResult() {
    if (a)
        return "a";
    return "b";
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?如果是这样,有人会证明吗?

编辑 这是可能的

编辑:最终 - 解决方案

这是我野蛮地黑客攻击的最终结果

Func<string> getResult = () =>
{
    switch (SC.Status)
    {
        case ServiceControllerStatus.Running:
            return "Running";
        case ServiceControllerStatus.Stopped:
            return "Stopped";
        case ServiceControllerStatus.Paused:
            return "Paused";
        case ServiceControllerStatus.StopPending:
            return "Stopping";
        case ServiceControllerStatus.StartPending:
            return "Starting";
        default:
            return "Status Changing";
    }
};

TrayIcon.Text = "Service Status - " + …
Run Code Online (Sandbox Code Playgroud)

c# variables function

10
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×2

.net ×1

anonymous-methods ×1

delegates ×1

function ×1

lambda ×1

variables ×1