将 Func 委托为 属性
Delegate Func as a property
如何将例如 2 个字符串传递给 Func 并 return 传递一个字符串?
假设我想传递 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"));
如何将例如 2 个字符串传递给 Func 并 return 传递一个字符串? 假设我想传递 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"));