Introduction to Actions

Overview

We already had an introduction to delegates and we saw how important they are in .NET applications. Because the concepts of delegates are very important, valuable, and useful in programming, the .NET Framework provides a special delegate, that is, pre-built. This delegate is named Action. The Action delegate is provided in many (overloaded) versions so that you can find a syntax that suits any void function or method. This means that in many cases, instead of creating a delegate from scratch, you can use the Action delegate instead.

A Simple Action

As we saw in our introduction, the simplest delegate is a method that doesn't use any parameter. Here is the example:

using static System.Console;

Title = "Living Quotes";

Observation quote = new Observation(HoldConference);

quote();

void HoldConference()
{
    WriteLine("There are known knowns. These are things we know that we know. " +
              "There are known unknowns. That is to say, there are things that " +
              "we know we don't know. But there are also unknown unknowns. " +
              "There are things we don't know we don't know.\n- Donald Rumsfeld -");
}

public delegate void Observation();

This would produce:

There are known knowns. These are things we know that we know. There are known unknowns. That is to say, there are things that we know we don't know. But there are also unknown unknowns. There are things we don't know we don't know.
- Donald Rumsfeld -

Press any key to close this window . . .

To support delegates that don't use any parameters, the .NET Framework provides the following version of the Action delegate:

public delegate void Action()

Based on this version of the Action delegate, you don't have to first create a delegate. In the section where you declare a variable for the delegate, use Action. This can be done as follows:

Action quote = new Action(HoldConference);

quote();

void HoldConference()
{
    Console.WriteLine("There are known knowns. These are things we know that we know. " +
                      "There are known unknowns. That is to say, there are things that " +
                      "we know we don't know. But there are also unknown unknowns. " +
                      "There are things we don't know we don't know.\n- Donald Rumsfeld -");
}

public delegate void Observation();

You don't need to use the new operator to initialize the variable. To start, you can omit calling Action() as a constructor. Here is an example:

Action quote = new(HoldConference);

quote();

void HoldConference()
{
    Console.WriteLine("There are known knowns. These are things we know that we know. " +
                      "There are known unknowns. That is to say, there are things that " +
                      "we know we don't know. But there are also unknown unknowns. " +
                      "There are things we don't know we don't know.\n- Donald Rumsfeld -");
}

public delegate void Observation();

You can also omit the new operator and its parentheses. Here is an example:

Action quote = HoldConference;

quote();

void HoldConference()
{
    Console.WriteLine("There are known knowns. These are things we know that we know. " +
                      "There are known unknowns. That is to say, there are things that " +
                      "we know we don't know. But there are also unknown unknowns. " +
                      "There are things we don't know we don't know.\n- Donald Rumsfeld -");
}

public delegate void Observation();

Actions and Anonymous Methods

Action delegates support anonymous methods. The anonymous delegate is created in the same way done for regular delegates. That is, initialyze the Action variable with the delegate keyword, the parentheses, and the body of the method in curly brackets. Here is an example:

Console.Title = "Action Delegates";
Console.WriteLine("Action Delegates");
Console.WriteLine("-----------------");

Action elementaryAlgebra = delegate ()
{
    Random rndNumber = new Random();
    int number1 = rndNumber.Next(0, 20);
    int number2 = rndNumber.Next(0, 20);
    int total = number1 + number2;

    Console.WriteLine("{0} + {1} = {2}", number1, number2, total);
};

elementaryAlgebra();

Console.WriteLine("=================================");

Here is an example of running the program:

Action Delegates
-----------------
9 + 12 = 21
=================================
Press any key to continue . . .

Actions and Lambda Expressions

Like regular delegates, actions support lambda expressions. To apply a lambda expression to an action delegate, initialize the Action variable with empty parentheses, the => operator, and the body of the method. Here is an example:

Console.Title = "Action Delegates";
Console.WriteLine("Action Delegates");
Console.WriteLine("-----------------");

Action elementaryAlgebra = () =>
{
    Random rndNumber = new Random();
    int number1 = rndNumber.Next(0, 20);
    int number2 = rndNumber.Next(0, 20);
    int total = number1 + number2;

    Console.WriteLine("{0} + {1} = {2}", number1, number2, total);
};

elementaryAlgebra();

Console.WriteLine("=================================");

A Delegate With a Parameter

We saw that a delegate can use a parameter. In fact, the Action delegate is created as generic. To support the ability to apply a parameter to a delegate, the .NET Framework provides an Action delegate that uses the following syntax:

public delegate void Action<in T>(T obj)

The parameter can be any type, such as a primitive type or a string. When declaring the variable, to indicate that the delegate is using a parameter, use Action<>. Include the parameter type between < and >. Here is an example:

Action<string> quote = new Action<string>(Create);

// A function for the delegate
void Create(string say)
{
    Console.WriteLine(say);
}

You can omit the second Action<string> expression. Here is an example:

Action<string> quote = new(Create);

quote("Les dances à la gloire de la fornication.");

void Create(string say)
{
    Console.WriteLine(say);
}

You can omit the new operator and its parentheses. Here is an example:

Action<string> quote = Create;

quote("Les dances à la gloire de la fornication.");

void Create(string say)
{
    Console.WriteLine(say);
}

An Action Delegate With Many Parameters

The Action delegate can use different numbers and types of parameters. To support this, the .NET Framework provides 17 versions of the Action delegate ranging from one that doesn’t use any parameter to one that takes 16 arguments. To use one of them, declare a variable of type Action<>. Between < and >, enter the types separated by commas. Initialize the variable with a method that use the same number or ordinal types of parameters.

An Action Delegate as a Type

Returning an Action

You can create a method that returns an Action delegate. As seen for formal delegates, when creating the method, set its return type as Action. In the body of the method, do what you want. Before the end of the method, return an object of the Action type. One solution is to declare a variable of Action type and initialize it appropriately. before calling the method, you can declare a variable and assign the Action method to it. Then call that last variable as if it were a method. Here is an example:

Action operation = Doubler();

operation();

Action Doubler()
{
    Action times = new Action(Multiplication);

    return times;
}

void Multiplication()
{
    Random rndNumber = new Random();
    int number1 = rndNumber.Next(0, 10);
    int number2 = rndNumber.Next(0, 10);
    int total = number1 * number2;

    Console.WriteLine(string.Format("{0} + {1} = {2}", number1, number2, total));
}

In the same way, a method can return a list or an array of Action delegates.

An Action as Parameter

To create a method that uses a parameter of type Action, precede that name of the parameter with Action. Here is an example:

void Create(Action speech)
{

}

In the same way, as opposed to passing it by itself, an Action delegate can be passed as argument along with other (types of) parameters. In the body of the method, ignore or use the Action parameter. Probably the simplest way to use it is to call it as a method. Here is an example:

void Create(Action speech)
{
    speech();
}

When calling the method, you can pass the name of a method that uses the same syntax as the type of Action that was passed as parameter (remember that the .NET Framework provides various syntaxes for actions). Here is an example:

void Hold()
{
    Console.WriteLine("People who live in glass houses should not throw stones.");
}

void Create(Action speech)
{
    speech();
}

Create(Hold);

This would produce:

People who live in glass houses should not throw stones.

Press any key to close this window . . .

.NET Function Delegates

Introduction

An Action is a delegate for a method that doesn’t produce a value. A function is an action that produces or returns a value. At a minimum, a function doesn't use any parameter.

Although you can create your own function delegates , to provide you a faster technique, the .NET Framework provides a special delegate named Func. It uses a generic format as in Func<>.

A Simple Function Delegate

Consider the following code:

Evaluation eval = new Evaluation(Calculate);

Console.WriteLine(string.Format("Number: {0}", eval().ToString("F")));

double Calculate()
{
    double hSalary = 25.75;
    double tWorked = 38.50;
    
    double wSalary = hSalary * tWorked;
    return wSalary;
}

// A delegate declaration
delegate double Evaluation();

This would produce:

Number: 991.38

Press any key to close this window . . .

A simple function is one that doesn't use any parameter but returns a value. To support such functions, the .NET Framework provides a Func<> delegate that uses the following syntax:

public delegate TResult Func<out TResult>()

The out TResult expression indicates that the delegate returns a value as TResult. When creating the function or method for the delegate, you must indicate the desired return value. To associate the function to the delegate, declare a Func<> variable. Between < and >, include the same return type as the function. Initialize the variable with the name of the function. You can then call the variable as if it were a method. This can be done as follows:

Func<double> eval = new Func<double>(Calculate);

Console.WriteLine(string.Format("Number: {0}", eval().ToString("F")));

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

    double wSalary = hSalary * tWorked;
    return wSalary;
}

In the above code, we used the new operator and the constructor to initialize the variable. You can omit the constructor after the new operator. Here is an example:

Func<double> eval = new(Calculate);

Console.WriteLine(string.Format("Number: {0}", eval().ToString("F")));

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

    double wSalary = hSalary * tWorked;
    return wSalary;
}

In fact, you can omit the new operator and its parentheses. Here is an example:

Func<double> eval = Calculate;

Console.WriteLine(string.Format("Number: {0}", eval().ToString("F")));

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

    double wSalary = hSalary * tWorked;
    return wSalary;
}

We already know that a function that returns one value can return it as a tuple instead. As we know already, tuples make it possible for a function to return many values. This concept also applies to function delegates. Here is an example:

Func<(string, double, string, double, double)> eval = Calculate;

Console.WriteLine(string.Format("{0}: {1}\n{2}: {3}\nNet Pay: {4}",
                eval().Item1,
                        eval().Item2.ToString(),
                        eval().Item3,
                        eval().Item4.ToString("F"),
                        eval().Item5.ToString()),
                        "Action Delegate");

(string, double, string, double, double) Calculate()
{
    double hSalary = 25.75;
    double tWorked = 38.50;

    double wSalary = hSalary * tWorked;
    return ("Hourly Salary", hSalary, "Time Worked", tWorked, wSalary);
}

This would produce:

Hourly Salary: 25.75
Time Worked: 38.50
Net Pay: 991.375

Press any key to close this window . . .

A Simple Anonymous Function Delegate

You can use the delegate keyword to create an anonymous function delegate. The technique is the same we have used so far. Here is an example:

Func<double> eval = delegate ()
{
    double hSalary = 25.75;
    double tWorked = 38.50;

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

Console.WriteLine(string.Format("Number {0}: ", eval().ToString("F")));

A Simple Lambda Expression

A function delegate that uses a parameter supports lambda expressions. To implement it, remove the delegate keyword and add the => operator after the parentheses. Here is an example:

Func<double> eval = () =>
{
    double hSalary = 25.75;
    double tWorked = 38.50;

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

Console.WriteLine(string.Format("Number: {0} ", eval().ToString("F")));

A Function Delegate with a Parameter

To support function delegates that use one parameter, the .NET Framework provides a delegate that uses the following syntax:

public delegate TResult Func<in T, out TResult>(T arg);

This syntax is for a function that takes one argument (and of course returns a value). Here is an example of such a function (and how to access it):

using static System.Console;

double hourlySalary = 12.75;

double Multiply(double timeWorked)
{
    if (timeWorked <= 8)
    {
        return hourlySalary * timeWorked;
    }
    else
    {
        double regularPay = 8.00 * hourlySalary;
        double overtimePay = (timeWorked - 8.00) * hourlySalary * 1.50;

        return regularPay + overtimePay;
    }
}

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

// A delegate initialization
Operation oper = Multiply;

double dailySalary = EvaluateSalary(oper);

WriteLine(string.Format("Daily Salary: {0}", dailySalary.ToString("F")));

delegate double Operation(double a);

This would produce:

Daily Salary: 192.84

Press any key to close this window . . .

To simplify your code, you can use a Func delegate. Instead of this:

using static System.Console;

double hourlySalary = 12.75;

double dailySalary = EvaluateSalary(timeWorked =>
{
    if (timeWorked <= 8)
    {
        return hourlySalary * timeWorked;
    }
    else
    {
        double regularPay = 8.00 * hourlySalary;
        double overtimePay = (timeWorked - 8.00) * hourlySalary * 1.50;

        return regularPay + overtimePay;
    }
});

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

WriteLine(string.Format("Daily Salary: {0}", dailySalary.ToString("F")));

delegate double Operation(double a);

You can use this:

using static System.Console;

double hourlySalary = 12.75;

Func<double, double> Calculate = new Func<double, double>(timeWorked =>
{
    if (timeWorked <= 8)
    {
        return hourlySalary * timeWorked;
    }
    else
    {
        double regularPay = 8.00 * hourlySalary;
        double overtimePay = (timeWorked - 8.00) * hourlySalary * 1.50;

        return regularPay + overtimePay;
    }
});

double dailySalary = Calculate(12.00);

WriteLine(string.Format("Daily Salary: {0}", dailySalary.ToString("F")));

This would produce:

Daily Salary: 178.50

Press any key to close this window . . .

Because the above function included many lines of code, we created a body for it and delimited it with curly brackets. If a function includes a single line of code, it doesn't need a body. Here is an example:

double hourlySalary = 12.75;

Func<double, double> Calculate = timeWorked => hourlySalary * timeWorked;

double dailySalary = Calculate(12.00);

Console.WriteLine(string.Format("Daily Salary: {0}", dailySalary.ToString("F")));

This would produce:

Daily Salary: 153.00

Press any key to close this window . . .

A Function Delegate as Type

Returning a Function Delegate

A function delegate is a type, like a value-type or a class/record/structure. As such, you can create a function or method that returns a function. To do this, specify Func<> as the return type. The parameter type can be a value type. Here is an example:

Func<double> Calculate()
{

}

Do whatever you want in the method but before it ends, return a value of type Func<>. Here is an example:

Func<int> Calculate()
{
    Func<int> result = Addition;

    return result;
}

int Addition()
{
    Random rndNumber = new Random();
    int number1 = rndNumber.Next(0, 20);
    int number2 = rndNumber.Next(0, 20);
    int total = number1 + number2;

    return total;
}

You can also just return the name of the function or method that returns the function delegate. Here is an example:

Func<int> Calculate()
{
    return Addition;
}

int Addition()
{
    Random rndNumber = new Random();
    int number1 = rndNumber.Next(0, 20);
    int number2 = rndNumber.Next(0, 20);
    int total = number1 + number2;

    return total;
}

When calling the method, you can assign it to a variable of type Func<>. Here is an example:

static Func<int> Calculate()
{
    Func<int> result = Addition;

    return result;
}

static int Addition()
{
    Random rndNumber = new Random();
    int number1 = rndNumber.Next(0, 20);
    int number2 = rndNumber.Next(0, 20);
    int total = number1 + number2;

    return total;
}

Func<int> eval = Calculate();

Console.WriteLine(string.Format("Daily Salary: {0}", eval()));

This would produce:

Daily Salary: 31

Press any key to close this window . . .

A Function Delegate as Parameter With One Parameter

We already know that you can pass a function as argument if you use a delegate. Here is an example:

double value = 148.86;

public double Multiply(double side)
{
    return side * side;
}

double Calculate(Operation oper)
{
    return oper(value);
}

double area = Calculate(Multiply);

Console.WriteLine(string.Format("Area of Cube: {0}", area));

delegate double Operation(double a);

This would produce:

Area of Cube: 22159.299600000006

Press any key to close this window . . .

When creating the method, instead of your own function, you can use Func<>. Here is an example for a function that takes one argument:

double value = 148.86;

double Multiply(double side)
{
    return side * side;
}

double Calculate(Func<double, double> oper)
{
    return oper(value) * 6.00;
}

double area = Calculate(Multiply);

Console.WriteLine(string.Format("Area of Cube: {0}", area));

This would produce:

Area of Cube: 132955.79760000005

Press any key to close this window . . .

Calling a One-Parameter Function Locally

When calling the method, instead of first creating a method that would be passed as argument, you can define the method directly where it is needed. Here is an example:

double value = 148.86;

double Calculate(Func<double, double> oper)
{
    return oper(value) * 6.00;
}

double area = Calculate((double side) =>
{
    return side * side;
});

Console.WriteLine(string.Format("Area of Cube: {0}", area));

Remember that you can omit the data type(s) of the parameter(s). Also remember that if the method is using one parameter, the parameter doesn't need parentheses. And if the method contains a single statement, it doesn't need a body. Here is an example:

double value = 148.86;

double Calculate(Func<double, double> oper)
{
    return oper(value) * 6.00;
}

double area = Calculate(side => side * side);

Console.WriteLine(string.Format("Area of Cube: {0}", area));

A Function Delegate with Two Parameters

Depending on your project, you may need a function that uses two parameters. To support this concept for function delegates, the .NET Framework provides the following version of the Func<> delegate:

public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);

This generic delegate uses three parameter types. The last parameter type,TResult, is the return type of the function. The first two types represent the data types of the parameters. In a simple version, they can be value types (number, character, Boolean) or a string. They can be the same or different types.

As seen with function delegates of one parameter, you can create a method that takes one parameter, you can create a method that takes a function of two parameters and, in the method, simply call the function and pass two arguments. You can use arguments you also pass to the method. Here is an example:

public double EvaluateSubTotal(Func<int, double, double> calc, int qty, double amount)
{
    return calc(qty, amount);
}

In this case, when calling the method, in the placeholder of the function passed as parameter, type the parentheses. In the parentheses, you must include two arguments separated by a comma. Outside the parentheses, type => followed by the desired operation for the arguments. Here is an example:

double subTotalShirtsSmall = hc.EvaluateSubTotal((q, a) => q * a,
						 iQuantityShirtsSmall, dUnitPriceShirtsSmall);

Alternatively, you can perform the operation in the method that uses the function delegate. Here is an example:

public double EvaluateSubTotal(Func<int, double, double> calc, int qty, double amount)
{
    return qty * amount;
}

Alternatively, you can define a function delegate where it is needed, then call it. Here is an example

using static System.Console;

double subTotal = 2425.75;
int shippingAndHanling = 36;

Func<double, double, double> Plus = (a, b) => a + b;
double invoiceTotal = Plus(subTotal, shippingAndHanling);

WriteLine(string.Format("Invoice Total: {0}", invoiceTotal));

This would produce:

Invoice Total: 2461.75

Press any key to close this window . . .

A .NET Function Delegate With Many Parameters

To support function delegates that can use various numbers and types of parameters, the .NET Framework provides 17 versions of the Func<> delegates. One version is for a function that uses no parameter. The other versions are for functions that take 1 to 16 arguments. The syntaxes of the function delegates are:

public delegate TResult Func<out TResult>();
public delegate TResult Func<in T, out TResult>(T arg);
public delegate TResult Func<in T1, in T2, out TResult>(T1 arg1, T2 arg2);
public delegate TResult Func<in T1, in T2, in T3, out TResult>(T1 arg1, T2 arg2, T3 arg3);
public delegate TResult Func<in T1, in T2, in T3, in T4, out TResult>(	T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, out TResult>(	T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11,  T12 arg12);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15);
public delegate TResult Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16);

As we saw already, for the first syntax, TResult is the only factor and it represents the return value. For the other syntaxes, the last parameter, TResult, is the return value. This means that, when declaring the Func<> variable, between < and >, start with the type(s) of the parameter(s) and end with the return type.

Everyone of these functions can be passed as argument or returned from a method.

Predicates

A predicate is a function that validates a conditions and presents the result as being true or false. To support predicates, the .NET library provides a function named Predicate. Its syntax is:

public delegate bool Predicate<in T>(T obj);

This is a generic function. The generic parameter can be anything type or class you want. This takes an argument of the type of the generic parameter. The function returns a Boolean value. Problably the easiest way to use this function is to first create a function that returns a Boolean value. This is an example:

bool Validate(string status)
{
    if (status == "Full-Time")
        return true;
    else
        return false;
}

To use the Predicate function, you can declare a variable of it with the type of the generic parameter of the function you would have created. Assign that function to the variable. You can then call that variable as a function and pass the value to examine to it. This can be done as follows:

string[] employee = { "275847", "Christine", "Palleo", "Full-Time",
                      "25.83", "9.00", "8.00", "8.50", "7.50", "9.50" };


double friday = double.Parse(employee[^1]);
double thursday = double.Parse(employee[^2]);
double wednesday = double.Parse(employee[^3]);
double tuesday = double.Parse(employee[^4]);
double monday = double.Parse(employee[^5]);
double timeWorked = monday + tuesday + wednesday + thursday + friday;
double netPay = double.Parse(employee[^6]) * timeWorked;
string emplName = employee[^9] + " " + employee[^8];
long emplNbr = long.Parse(employee[^10]);

Predicate<string> predicate = Validate;

bool result = predicate(employee[^7]);

Console.WriteLine("===================================================");
Console.WriteLine("     - Payroll Preparation -");
Console.WriteLine("===================================================");
Console.WriteLine("Employee Information");
Console.WriteLine("---------------------------------------------------");
Console.WriteLine($"Employee #:            {emplNbr}");
Console.WriteLine($"Employee Name:         {emplName}");
Console.WriteLine("Employed Full-Time:     {0}", result);
Console.WriteLine($"Hourly Salary:         {double.Parse(employee[^6]):f}");
Console.WriteLine("===================================================");
Console.WriteLine("Time Worked");
Console.WriteLine("---------------------------------------------------");
Console.WriteLine($"Monday:     {monday,16:f}");
Console.WriteLine($"Tuesday:    {tuesday,16:f}");
Console.WriteLine($"Wednesday:  {wednesday,16:f}");
Console.WriteLine($"Thursday:   {thursday,16:f}");
Console.WriteLine($"Friday:     {friday,16:f}");
Console.WriteLine("---------------------------------------------------");
Console.WriteLine($"Total Time: {timeWorked,16:F}");
Console.WriteLine($"Net Pay:    {netPay,16:F}");
Console.WriteLine("===================================================");

bool Validate(string status)
{
    if (status == "Full-Time")
        return true;
    else
        return false;
}

This would produce:

===================================================
     - Payroll Preparation -
===================================================
Employee Information
---------------------------------------------------
Employee #:            275847
Employee Name:         Christine Palleo
Employed Full-Time:    True
Hourly Salary:         25.83
===================================================
Time Worked
---------------------------------------------------
Monday:                 9.00
Tuesday:                8.00
Wednesday:              8.50
Thursday:               7.50
Friday:                 9.50
---------------------------------------------------
Total Time:            42.50
Net Pay:             1097.77
===================================================

Press any key to close this window . . .

The way the Predicate function is usually used is with methods that need to verify some conditions. The syntaxes of such methods indicate what argument needs to be checked and what result is expected. When you deal with those methods, you will know what needs to be done.


Previous Copyright © 2008-2024, FunctionX Thursday 21 October 2021 Next