如何将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; }
}
你能帮我解决这个问题吗?
答案 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"));