简体   繁体   English

委托Func作为财产

[英]Delegate Func as a property

How can I pass eg 2 strings to Func and return a string? 如何将2个字符串传递给Func并返回一个字符串? Let say I would like to pass FirstName and LastName and the result should be like FirstName + LastName; 假设我想传递FirstName和LastName,结果应该像FirstName + LastName;

Moreover, I would like to have a Func declared as a property. 而且,我想将Func声明为属性。

Please take a look at my code: 请看一下我的代码:

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; }
}

Could you please help me to solve this problem? 你能帮我解决这个问题吗?

This should do the trick: 这应该做的伎俩:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM