mskuratowski
mskuratowski

Reputation: 4124

Delegate Func as a property

How can I pass e.g 2 strings to Func and return a string? Let say I would like to pass FirstName and LastName and the result should be like FirstName + LastName;

Moreover, I would like to have a Func declared as a property.

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?

Upvotes: 6

Views: 13329

Answers (1)

Himzo Tahic
Himzo Tahic

Reputation: 151

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

Upvotes: 9

Related Questions