Integer Array OperationOperate Method
Applies the specified function to the given array of operands.
Definition
Namespace: SampleClassLibrary.Advanced
Assembly: SampleClassLibrary (in SampleClassLibrary.dll) Version: 1.0.0+3172fc1e8edc6022677e793f139f3a9031307055
The results of the operations.
Assembly: SampleClassLibrary (in SampleClassLibrary.dll) Version: 1.0.0+3172fc1e8edc6022677e793f139f3a9031307055
C#
public static Task<int[][]> Operate(
Func<int, int>[] funcs,
int[] operands
)VB
Public Shared Function Operate (
funcs As Func(Of Integer, Integer)(),
operands As Integer()
) As Task(Of Integer()())C++
public:
static Task<array<array<int>^>^>^ Operate(
array<Func<int, int>^>^ funcs,
array<int>^ operands
)F#
static member Operate :
funcs : Func<int, int>[] *
operands : int[] -> Task<int[][]> Parameters
- funcs FuncInt32, Int32
- The functions to evaluate at each operand.
- operands Int32
- The array of operands.
Return Value
TaskInt32The results of the operations.
Example
In the following example, integers in a given array are both squared and doubled by executing the Operate(FuncInt32, Int32, Int32) method. In addition, input validation is also checked.
C#
using System;
using System.Threading.Tasks;
using SampleClassLibrary.Advanced;
namespace SampleClassLibrary.CodeExamples.Advanced
{
/// <summary>
/// An example showing how to exploit class <see cref="IntegerArrayOperation"/>.
/// </summary>
public class IntegerArrayOperationExample
{
/// <summary>
/// The method encapsulating the code to be exemplified.
/// </summary>
public async Task Main()
{
// Define an operator that squares its operand
Func<int, int> square = (int operand) => operand * operand;
// Define an operator that doubles its operand
Func<int, int> doubleIt = (int operand) => 2 * operand;
// Define an array of operands
int[] operands = new int[3] { 2, 4, 8 };
// Operate on it
int[][] results = await IntegerArrayOperation.Operate([square, doubleIt], operands);
// Show results
for (int j = 0; j < results.Length; j++)
{
for (int i = 0; i < results[j].Length; i++)
{
Console.WriteLine(
"The result of {0} {1} is {2}.",
j == 0 ? "squaring" : "doubling",
operands[i],
results[j][i]);
}
}
// Check that an operator cannot be null
try
{
await IntegerArrayOperation.Operate(null, new int[1]);
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine("Cannot apply a null function:");
Console.WriteLine(e.Message);
}
// Check that an array of operands cannot be null
try
{
await IntegerArrayOperation.Operate([square, doubleIt], null);
}
catch (Exception e)
{
Console.WriteLine();
Console.WriteLine("Cannot apply a function to a null array:");
Console.WriteLine(e.Message);
}
}
}
}
// Executing method Main() produces the following output:
//
// The result of squaring 2 is 4.
// The result of squaring 4 is 16.
// The result of squaring 8 is 64.
// The result of doubling 2 is 4.
// The result of doubling 4 is 8.
// The result of doubling 8 is 16.
//
// Cannot apply a null function:
// Value cannot be null. (Parameter 'funcs')
//
// Cannot apply a function to a null array:
// Value cannot be null. (Parameter 'operands')Exceptions
| ArgumentNullException | funcs is null. -or- operands is null. |