Integer OperationOperate Method
Applies the specified function to the given operand.
Definition
Namespace: SampleClassLibrary
Assembly: SampleClassLibrary (in SampleClassLibrary.dll) Version: 1.0.0+3172fc1e8edc6022677e793f139f3a9031307055
The result of the operation.
Assembly: SampleClassLibrary (in SampleClassLibrary.dll) Version: 1.0.0+3172fc1e8edc6022677e793f139f3a9031307055
C#
public static int Operate(
Func<int, int> func,
int operand
)VB
Public Shared Function Operate (
func As Func(Of Integer, Integer),
operand As Integer
) As IntegerC++
public:
static int Operate(
Func<int, int>^ func,
int operand
)F#
static member Operate :
func : Func<int, int> *
operand : int -> int Parameters
Return Value
Int32The result of the operation.
Example
In the following example, an integer is squared executing the Operate(FuncInt32, Int32, Int32) method. In addition, input validation is also checked.
C#
using System;
namespace SampleClassLibrary.CodeExamples
{
/// <summary>
/// An example showing how to exploit class <see cref="IntegerOperation"/>.
/// </summary>
public class IntegerOperationExample
{
/// <summary>
/// The method encapsulating the code to be exemplified.
/// </summary>
public void Main()
{
// Define an operator that squares its operand
Func<int, int> square = (int operand) => operand * operand;
// Define an operand
int integer = 2;
// Operate on it
Console.WriteLine("Squaring {0}...", integer);
int result = IntegerOperation.Operate(square, integer);
Console.WriteLine("...the result is {0}.", result);
// Check that an operator cannot be null
try
{
IntegerOperation.Operate(null, 0);
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine("Cannot apply a null function:");
Console.WriteLine(e.Message);
}
}
}
}
// Executing method Main() produces the following output:
//
// Squaring 2...
// ...the result is 4.
//
// Cannot apply a null function:
// Value cannot be null. (Parameter 'func')Exceptions
| ArgumentNullException | func is null. |