c# - 在 C# 的同一类中的另一个委托(delegate)函数中使用两个委托(delegate)函数的返回 View

标签 c# asp.net asp.net-mvc delegates

我在类中定义了 3 个委托(delegate)函数,例如

public func<datetime,string>A=>str1 { }
public func<datetime,string>B=>str2 { }
public func<datetime,string>C=>str3
{
    return a+b; 
}

我怎么能在c函数中调用a,b,因为函数c中的return是错误的?

最佳答案

由于多种原因,您提供的代码无法编译。要调用 AB 方法,您必须正确调用它并传递如下参数:

public static Func<DateTime, string> A = (date) => { return date.ToString(); };
public static Func<DateTime, string> B = (date) => { return date.ToString(); };
public static Func<DateTime, string> C = (date) => { return A(date) + B(date); };

static void Main(string[] args)
{
    var date = DateTime.Now;
    Console.WriteLine(C(date));
}

看到我还添加了 static 因为没有它你会得到错误:

A field initializer cannot reference the non-static field, method, or property Program.A

关于c# - 在 C# 的同一类中的另一个委托(delegate)函数中使用两个委托(delegate)函数的返回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43774304/

相关文章:

asp.net-mvc - 使用部分GUID作为ID

c# - 在 MVC 中保存数据

c# - ASP.Net Core 的自定义不记名 token 授权

ASP.NET MVC-有没有一种方法可以模拟ViewState?

c# - 将方法概括为 'Get' 任何对象属性

asp.net - iis 8.5 中用于 mvc 和 webapi 应用程序的路由

c# - ASP HttpWebRequest 和重定向

asp.net-mvc - 使用循环方法对 nopcommerce 进行负载平衡

c# - .NET Web 服务 - 如何返回错误?

C#绘图消失(其实更多的是系统问题)