一旦编译完成,它们之间是否存在差异:
delegate { x = 0; }
Run Code Online (Sandbox Code Playgroud)
和
() => { x = 0 }
Run Code Online (Sandbox Code Playgroud)
?
在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)