c# - 将 Func 委托(delegate)为属性

标签 c# .net delegates .net-4.0 func

如何将例如 2 个字符串传递给 Func 并返回一个字符串? 假设我想传递 FirstName 和 LastName,结果应该类似于 FirstName + LastName;

此外,我想将 Func 声明为属性。

请看我的代码:

public class FuncClass
{
    private string FirstName = "John";
    private string LastName = "Smith";
    //TODO: declare FuncModel and pass FirstName and LastName.
}

public class FuncModel
{
    public Func<string, string> FunctTest { get; set; }
}

你能帮我解决这个问题吗?

最佳答案

这应该可以解决问题:

public class FuncModel
{
    //Func syntax goes <input1, input2,...., output>
    public Func<string, string, string> FunctTest { get; set; }
}

var funcModel = new FuncModel();
funcModel.FunctTest = (firstName, lastName) => firstName + lastName;
Console.WriteLine(funcModel.FuncTest("John", "Smith"));

关于c# - 将 Func 委托(delegate)为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47062624/

相关文章:

c# - 为什么 BeginInvoke 不返回 AsyncResult 类型的引用?

swift - 从 tableView 中删除 StructItem

ios - 标签栏 : interaction between tabs

c# - 异步/等待与线程

c# - 如何设置 VS 在非 UITest 项目中使用 ImageComparer?

c# - 如何更改 Windows 控件的 `All Desired` 属性?

c# - 我应该更喜欢封闭式还是开放式列表<>系统?

c# - 如何通过单击按钮向上或向下移动 ListView 项目

c# - 如何获得 child 控制权?

c# - 如何在 C# 中为泛型委托(delegate)声明泛型事件