委托Func作为财产

时间:2017-11-01 19:50:43

标签: 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; }
}

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

1 个答案:

答案 0 :(得分:2)

这应该可以解决问题:

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"));