Lambda function return type

Key points

				
					#nullable enable
public sealed class LambdaFuncRetType
{
    public static void Main(string[] args)
    {

        //want to return string and integer both from the method
        //var ageValue0 =  (gender) =>
        //{
        //    if (gender == "Male") return 1;
        //    if (gender == "Female") return 0;
        //    else return "not valid gender";
        //};

        //1st way
        var ageValue1 = object (string gender) =>
        {
            if (gender == "Male") return 1;
            if (gender == "Female") return 0;
            else return "not valid gender";
        };

        //2nd way
        Func<string,object> ageValue2 =  (gender) =>
        {
            if (gender == "Male") return 1;
            if (gender == "Female") return 0;
            else return "not valid gender";
        };
    }
}




				
			
  • If we want to return more than one return type from the lambda method.
  • Two ways as shown in above example
    • var ageValue1 = object (string gender) =>
    • Func<string,object> ageValue2 = (gender) =>

Leave a Comment