A Delegate as a Function

Introduction

A function is an action that formally produces a value. We say that a function returns a value. In the same way, you can create a delegate that returns a value. To formally support this concept, the .NET Framework provides special built-in delegates.

Introduction to Creating a Function Delegate

The basic formula used to create a delegate that returns a value is:

[attributes] [modifier(s)] delegate return-type Name ([formal-parameters]);

When creating the delegate, specify the data type to the left side of the name of the delegate. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Evaluation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

When defining a method that would be associated with the delegate, the method must return the same type of value. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Evaluation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {

            return View();
        }

        public double Calculate()
        {
            return 0.00;
        }
    }
}

Using a Function Delegate

To use the method, you can first declare a variable of the type of the delegate and initialize it using its default constructor. In the parentheses of the constructor, pass the name of the method. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Evaluation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Evaluation eval = new Evaluation(Calculate);

            return View();
        }

        public double Calculate()
        {
            double hSalary = 25.75;
            double tWorked = 38.50;

            double wSalary = hSalary * tWorked;
            return wSalary;
        }
    }
}

To use the delegate, you can call the variable as if it were a method. If you need the value returned by the delegate, declare another variable and initialize with a call to the delegate variable. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Evaluation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Evaluation eval = new Evaluation(Calculate);

            double weeklySalary = eval();

            return View();
        }

        public double Calculate()
        {
            double hSalary = 25.75;
            double tWorked = 38.50;

            double wSalary = hSalary * tWorked;
            return wSalary;
        }
    }
}

If you wrote your code in a class, one way you can automatically send the value to a webpage is to assign the value to a property applied to an anonymous object. This can be done as follows:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Evaluation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Evaluation eval = new Evaluation(Calculate);

            ViewBag.WeeklySalary = eval().ToString("F");
            
            return View();
        }

        public double Calculate()
        {
            double hSalary = 25.75;
            double tWorked = 38.50;

            double wSalary = hSalary * tWorked;
            return wSalary;
        }
    }
}

Here an example of accessing the value in a webpage:

@{
    ViewBag.Title = "Payroll Preparation";
}

<p>Weekly Salary: @ViewBag.WeeklySalary</p>

When initializing the delegate variable, an alternative is to assign the name of the method to the delegate variable. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Evaluation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Evaluation eval = new Evaluation(Calculate);

            ViewBag.WeeklySalary = eval().ToString("F");

            return View();
        }

        public double Calculate()
        {
            double hSalary = 25.75;
            double tWorked = 38.50;

            double wSalary = hSalary * tWorked;
            return wSalary;
        }
    }
}

You can access or call the delegate in the HTML side of a webpage. In this case, create the delegate and the associated method in the class. This can be done as follows:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public double Multiply()
        {
            const double hourlySalary = 25.75;
            const double timeWorkedWeek = 38.50;

            return hourlySalary * timeWorkedWeek;
        }
    }
}

In the HTML side of the webpage, declare a variable for the delegate and initialize it as done previously. Here is an example:

@{
    Exercises.Controllers.HomeController hc = new Exercises.Controllers.HomeController();
    Exercises.Controllers.Operation calc = new Exercises.Controllers.Operation(hc.Multiply);
}

To access the delegate, call its variable like a method. Since the delegate returns a value, you can assign the call to variable and use that value as you see fit. Here is an example:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.HomeController hc = new Exercises.Controllers.HomeController();
    Exercises.Controllers.Operation calc = new Exercises.Controllers.Operation(hc.Multiply);

    double salary = calc();
}

<p>Weekly Salary: @salary.ToString("F")</p>

If you are not planning to use the value many times, you can call the variable directly where you need it. Here are examples:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.HomeController hc = new Exercises.Controllers.HomeController();
    Exercises.Controllers.Operation calc = new Exercises.Controllers.Operation(hc.Multiply);
}

<p>Weekly Salary: @calc().ToString("F")</p>

A Function Delegate with a Parameter

A delegate that returns a value can also use one or more parameters. Here is an example of creating such a delegate:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double nbr);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

Once again, you must have a method to associate to the delegate. When creating the method, it must have the same syntax as the delegate. In the body of the method, use or ignore the argument. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double nbr);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public double Calculate(double sal)
        {
            return 0.00;
        }
    }
}

Once again, before using the delegate, you can declare a variable and initialize it with the method. Here are two examples of doing it:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double nbr);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Operation oper = new Operation(Calculate);
            Operation eval = Calculate;

            return View();
        }

        public double Calculate(double sal)
        {
            const double tWorked = 40.00;

            return sal * tWorked;
        }
    }
}

When calling the delegate variable, you can declare a variable that is the type of the return value of the delegate. Call the delegate variable as if it were a method. In its parenthese, pass the appropriate number and type(s) of argument(s). Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double nbr);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Operation oper = new Operation(Calculate);

            double hourlySalary = 25.75;

            ViewBag.WeeklySalary = eval(hourlySalary).ToString("F");

            return View();
        }

        public double Calculate(double sal)
        {
            const double tWorked = 40.00;

            return sal * tWorked;
        }
    }
}

Of course, you can also pass a value or a constant directly to the variable call.

A Function Delegate with Many Parameters

A delegate that returns a value can also use many parameters. The parameters can be of the same or different types. The associated method must use the same number and types of parameters. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double x, double y);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public double Add(double number1, double number2)
        {
            return number1 + number2;
        }
    }
}

Function Delegates and Anonymous Methods

Introduction

Function delegates support anonymous methods. You primarily create the method as done for a void delegate. This time, since the method must produce a value, you can (or must) use its last expression to return a value. Here is an example:

@{
    Exercises.Controllers.Operation calc = delegate ()
    {
        const double hourlySalary = 25.75;
        const double timeWorkedWeek = 38.50;

        return hourlySalary * timeWorkedWeek;
    };
}

After doing this, you can just call the variable as done for a method, but if you want to get or use the returned value of the mehod, assign the call to another variable. Here is an example:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation calc = delegate ()
    {
        const double hourlySalary = 25.75;
        const double timeWorkedWeek = 38.50;

        return hourlySalary * timeWorkedWeek;
    };

    double salary = calc();
}

<p>Weekly Salary: @salary.ToString("F")</p>

As mentioned previously, you can also call the variable directly where it is needed.

Using a Lambda Expression

Instead of first creating a formal method, you can use a local lambda expression. To do this, from the above code, simply omit the delegate keyword. Here is an example:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation calc = () =>
    {
        const double hourlySalary = 25.75;
        const double timeWorkedWeek = 38.50;

        return hourlySalary * timeWorkedWeek;
    };

    double salary = calc();
}

<p>Weekly Salary: @salary.ToString("F")</p>

Remember that you can omit the data types of the arguments. Remember that you don't have to get or use the value from the method.

Anonymous Function Delegates and Parameters

To create an anonymous delegate that uses a parameter, provide the data type and name of the parameter in the parentheses. Here is an example that uses the delegate keyword:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation times = delegate (double hSalary)
    {
        const double timeWorkedWeek = 38.50;

        return hSalary * timeWorkedWeek;
    };

    double wSalary = times(32.85);
}

<p>Weekly Salary: @wSalary.ToString("F")</p>

Remember that a delegate can use more than one parameter. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a, double b);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

When creating the anonymous delegate, pass the same number and types of parameters. Here is an example:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation times = delegate (double hSalary, double time)
    {
        return hSalary * time;
    };

    double wSalary = times(, 30.25, 44.00);
}

<p>Weekly Salary: @wSalary.ToString("F")</p>

You can omit the delegate keyword, in which case you must add the => operator after the parentheses. Here is an example that uses one parameter:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation times = (double hSalary) =>;
    {
        const double timeWorkedWeek = 38.50;

        return hSalary * timeWorkedWeek;
    };

    double wSalary = times(32.85);
}

<p>Weekly Salary: @wSalary.ToString("F")</p>

In the case you are not using the delegate keyword, you can omit the data type(s) of the parameter(s). Here is an example that uses one parameter:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation times = (hSalary) =>;
    {
        const double timeWorkedWeek = 38.50;

        return hSalary * timeWorkedWeek;
    };

    double wSalary = times(32.85);
}

<p>Weekly Salary: @wSalary.ToString("F")</p>

Here is an example that uses two parameters:

@{
    ViewBag.Title = "Payroll Preparation";
}

@{
    Exercises.Controllers.Operation times = (hSalary, time) =>
    {
        return hSalary * time;
    };

    double wSalary = times(30.25, 44.00);
}

<p>Weekly Salary: @wSalary.ToString("F")</p>

A Delegate as a Parameter

Passing a Parameter-Less Delegate as Parameter

Because a delegate is an object, it can be used as a parameter. Of course, you should start by having a delegate. Then create a method that that uses a parameter of the type of that delegate.Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public double Calculate(Operation oper)
        {
            return 0.00;
        }
    }
}

In the body of the method, you can use or ignore the parameter. When calling the method, pass to it a method that uses the same syntax as the delegate. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation();

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            Operation eval = new Operation(Multiply);

            double triple = Calculate(Multiply);
            ViewBag.MonthlySalary = triple;

            return View();
        }

        public double Multiply()
        {
            const double hourlySalary = 25.75;
            const double timeWorkedWeek = 38.50;

            return hourlySalary * timeWorkedWeek;
        }

        public double Calculate(Operation oper)
        {
            double number = oper();

            double result = number * 4.00;
            return result;
        }
    }
}

Passing a One-Parameter Delegate as Parameter

You can create a method that takes a delegate as parameter and that delegate may take one parameter. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class HomeController : Controller
    {
        private double hourlySalary;

        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public double Calculate(Operation oper)
        {
            return 0.00;
        }
    }
}

When calling the method, pass a method that uses the same syntax as the delegate. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class HomeController : Controller
    {
        private double hourlySalary;

        // GET: Home
        public ActionResult Index()
        {
            hourlySalary = 28.55;

            double triple = Calculate(Multiply);
            
            ViewBag.MonthlySalary = triple;

            return View();
        }

        public double Multiply(double hSal)
        {
            const double timeWorkedWeek = 38.50;

            return hSal * timeWorkedWeek;
        }

        public double Calculate(Operation oper)
        {
            double number = oper(hourlySalary);

            double result = number * 4.00;
            return result;
        }
    }
}

Passing a Multi-Parameter Delegate as Parameter

You can create a method that takes a multi-parameters delegate as parameter. Here is an example of such a delegate:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a, double b);

    public class HomeController : Controller
    {
        private double timeWorked;
        private double hourlySalary;

        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        public double Calculate(Operation oper)
        {
            return 0.00;
        }
    }
}

When calling the method, pass the name of a method that uses the same syntax as the delegate. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a, double b);

    public class HomeController : Controller
    {
        private double timeWorked;
        private double hourlySalary;

        // GET: Home
        public ActionResult Index()
        {
            timeWorked = 44.50;
            hourlySalary = 28.55;

            double quadruple = Calculate(Multiply);
            
            ViewBag.MonthlySalary = quadruple;

            return View();
        }

        public double Multiply(double hSal, double work)
        {
            return hSal * work;
        }

        public double Calculate(Operation oper)
        {
            double number = oper(hourlySalary, timeWorked);

            double result = number * 4.00;
            return result;
        }
    }
}

In the above examples, we used values that were external to the method to perform the necessary calculations. As an alternative, you can pass (an) additional parameter(s) that the delegate-parameter would use to perform the calculations. Here is an example for a method that uses a delegate of one parameter:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            double hourlySalary = 28.55;

            double triple = Calculate(Multiply, hourlySalary);

            ViewBag.WeeklySalary = triple;

            return View();
        }

        public double Multiply(double hSal)
        {
            return hSal * 40.00;
        }

        public double Calculate(Operation oper, double number)
        {
            double result = oper(number);
            
            return result;
        }
    }
}

In the same way, if a multi-parameter delegate is passed to a method, you can pass all the other pamameters that the delegate must process. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a, double b);

    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            double timeWorked = 42.00;
            double hourlySalary = 28.55;

            double triple = Calculate(Multiply, hourlySalary, timeWorked);

            ViewBag.WeeklySalary = triple;

            return View();
        }

        public double Multiply(double hSal, double time)
        {
            return hSal * time;
        }

        public double Calculate(Operation oper, double number, double value)
        {
            double result = oper(number, value);
            
            return result;
        }
    }
}

Using a Lambda Expression

A lambda expression can take a delegate as parameter. Of course, you should (must) have a delegate. You can first create one in the class. Here is an example:

using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double ThreeOperands(double x, double y, bool z);

    public class ConversationController : Controller
    {
        // GET: Conversation
        public ActionResult Index()
        {
            return View();
        }
    }
}

As we saw already, you can then create a method that receives such a delegate as argument. We saw that, to call the method, you can pass the name of a method that uses the same syntax as the delegate. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation();

    public class SalaryEvaluationController : Controller
    {
        private double hourlySalary = 12.74;
        private double timeWorked = 10.50;

        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            Operation oper = Multiply;

            double dailySalary = EvaluateSalary(oper);

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double Multiply()
        {
            if(timeWorked <= 8)
            {
                return hourlySalary * timeWorked;
            }
            else
            {
                double overtime = timeWorked - 8.00;
                double overtimeRate = hourlySalary * 1.50;

                double regularPay = 8.00 * hourlySalary;
                double overtimePay = overtime * overtimeRate;
                return regularPay + overtimePay;
            }
        }

        public double EvaluateSalary(Operation calc)
        {
            return calc();
        }
    }
}

Here is an example of accessing the value that was created in the code:

@{
    ViewBag.Title = "Salary Evaluation";
}

<h2>Salary Evaluation</h2>

<p>Daily Salary: @ViewBag.DailySalary</p>

When calling the method that takes a delegate as parameter, instead of first creating the method that would be associated to the delegate, you can implement a lambda expression directly in the parentheses of the calling method as lamda expression. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation();

    public class SalaryEvaluationController : Controller
    {
        private double hourlySalary = 12.74;
        private double timeWorked = 10.50;

        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            double dailySalary = EvaluateSalary(() =>
            {
                if (timeWorked <= 8)
                {
                    return hourlySalary * timeWorked;
                }
                else
                {
                    double overtime = timeWorked - 8.00;
                    double overtimeRate = hourlySalary * 1.50;

                    double regularPay = 8.00 * hourlySalary;
                    double overtimePay = overtime * overtimeRate;
                    return regularPay + overtimePay;
                }
            });

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double EvaluateSalary(Operation calc)
        {
            return calc();
        }
    }
}

You can also create a method that takes a delegate that uses one parameter. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class SalaryEvaluationController : Controller
    {
        private double hourlySalary = 12.75;

        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            Operation oper = Multiply;

            double dailySalary = EvaluateSalary(oper);

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double Multiply(double timeWorked)
        {
            if (timeWorked <= 8)
            {
                return hourlySalary * timeWorked;
            }
            else
            {
                double overtime = timeWorked - 8.00;
                double overtimeRate = hourlySalary * 1.50;

                double regularPay = 8.00 * hourlySalary;
                double overtimePay = overtime * overtimeRate;
                return regularPay + overtimePay;
            }
        }

        public double EvaluateSalary(Operation calc)
        {
            return calc(hourlySalary);
        }
    }
}

Instead of first creating a method to associate to the delegate, you can implement the method directly where it is needed. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class SalaryEvaluationController : Controller
    {
        private double hourlySalary = 12.74;

        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            double dailySalary = EvaluateSalary((double timeWorked) =>
	        {
                if (timeWorked <= 8)
                {
                    return hourlySalary * timeWorked;
                }
                else
                {
                    double overtime = timeWorked - 8.00;
                    double overtimeRate = hourlySalary * 1.50;

                    double regularPay = 8.00 * hourlySalary;
                    double overtimePay = overtime * overtimeRate;
                    return regularPay + overtimePay;
                }
            });

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double EvaluateSalary(Operation calc)
        {
            return calc(hourlySalary);
        }
    }
}

Remember that you can omit the data type of the argument in the lambda expression. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class SalaryEvaluationController : Controller
    {
        private double hourlySalary = 12.74;

        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            double dailySalary = EvaluateSalary((timeWorked) =>
	        {
                if (timeWorked <= 8)
                {
                    return hourlySalary * timeWorked;
                }
                else
                {
                    double overtime = timeWorked - 8.00;
                    double overtimeRate = hourlySalary * 1.50;

                    double regularPay = 8.00 * hourlySalary;
                    double overtimePay = overtime * overtimeRate;
                    return regularPay + overtimePay;
                }
            });

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double EvaluateSalary(Operation calc)
        {
            return calc(hourlySalary);
        }
    }
}

In most examples above, we first declared some variables and used them when calling the method. Normally, the values may not be available before the method is called. The solution is to pass additional parameters that would accompagny the method that would use them. Here is an example for a regular delegate:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a, double b);

    public class SalaryEvaluationController : Controller
    {
        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            Operation oper = Multiply;

            double dailySalary = EvaluateSalary(12.74, 10.50, oper);

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double Multiply(double hSalary, double time)
        {
            if(time <= 8)
            {
                return hSalary * time;
            }
            else
            {
                double overtime = time - 8.00;
                double overtimeRate = hSalary * 1.50;

                double regularPay = 8.00 * hSalary;
                double overtimePay = overtime * overtimeRate;
                return regularPay + overtimePay;
            }
        }

        public double EvaluateSalary(double x, double y, Operation calc)
        {
            return calc(x, y);
        }
    }
}

In the same way, if you are creating a lambda expression, you can pass (an) addition parameter(s). Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a, double b);

    public class SalaryEvaluationController : Controller
    {
        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            double dailySalary = EvaluateSalary(12.74, 10.50,
                                                (double hSalary, double time) =>
	        {
                if (time <= 8.00)
                {
                    return hSalary * time;
                }
                else
                {
                    double overtime = time - 8.00;
                    double overtimeRate = hSalary * 1.50;

                    double regularPay = 8.00 * hSalary;
                    double overtimePay = overtime * overtimeRate;
                    return regularPay + overtimePay;
                }
            });

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double EvaluateSalary(double x, double y, Operation calc)
        {
            return calc(x, y);
        }
    }
}

Remember that you can omit the data types of the parameters in the lambda expression.

If the lambda expression is only only one parameter, you can omit the parentheses of the parameter. Here is an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Exercises.Controllers
{
    public delegate double Operation(double a);

    public class SalaryEvaluationController : Controller
    {
        private double hourlySalary = 12.74;

        // GET: SalaryEvaluation
        public ActionResult Index()
        {
            double dailySalary = EvaluateSalary(timeWorked =>
	        {
                if (timeWorked <= 8)
                {
                    return hourlySalary * timeWorked;
                }
                else
                {
                    double overtime = timeWorked - 8.00;
                    double overtimeRate = hourlySalary * 1.50;

                    double regularPay = 8.00 * hourlySalary;
                    double overtimePay = overtime * overtimeRate;
                    return regularPay + overtimePay;
                }
            });

            ViewBag.DailySalary = dailySalary.ToString("F");

            return View();
        }

        public double EvaluateSalary(Operation calc)
        {
            return calc(hourlySalary);
        }
    }
}

If the lambda expression is using more than one parameter, those parameters must be included in parentheses.


Previous Copyright © 2008-2019, FunctionX Next