/// <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); } }
public void ShouldBeAbleToAddNewMethod() { IntegerOperation addBody = delegate(int a, int b) { return(a + b); }; var dynamic = new DynamicObject(new object()); dynamic.AddMethod("Add", addBody); var result = (int)dynamic.Methods["Add"](1, 1); Assert.AreEqual(2, result); }
static void Main(string[] args) { IntegerOperation op0 = Sum; op0(1, 2); Console.WriteLine(); op0 += Diff; op0(2, 3); Console.WriteLine(); MultyTypeOperation <double, double> op1 = Concatenate; MultyTypeOperation <int, int> op2 = Concatenate; op1.Invoke(1.1, 2.2); op2.Invoke(1, 2); Console.WriteLine(); Action <int, int> op3 = Concatenate; op3(3, 4); Console.WriteLine(); Predicate <int> IsPositive = (x) => x > 0; Console.WriteLine(IsPositive(6)); Console.WriteLine(); Func <int, int, double> op4 = (x, y) => { return((double)x + (double)y); }; op4(2, 3); if (DateTime.Now.Hour < 12) { Show_Message(GoodMorning); } else { Show_Message(GoodEvening); } Console.ReadKey(); }
/// <summary> /// Applies the specified function to the given array of operands. /// </summary> /// <param name="func">The function to evaluate at each operand.</param> /// <param name="operands">The array of operands.</param> /// <returns>The results of the operations.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="func"/> is <b>null</b>.<br/> /// -or-<br/> /// <paramref name="operands"/> is <b>null</b>. /// </exception> /// <example> /// <para> /// In the following example, the applied function, say /// <latex mode='inline'>f:\mathbb{N}\rightarrow \mathbb{N},</latex> is defined as /// <latex mode='display'> /// \forall n \in \mathbb{N}: n \mapsto f\left(n\right)=n^2. /// </latex> /// A plot of such function is displayed in the following /// figure. /// <image> /// <alt>A function.</alt> /// <src>Function.png</src> /// <width>100%</width> /// </image> /// Integers in a given array are thus squared /// executing the <see cref="Operate(Func{int, int}, int[])"/> method. /// In addition, input validation is also checked. /// </para> /// <para> /// <code language="cs"> /// using System; /// using SampleClassLibrary.Advanced; /// /// namespace SampleClassLibrary.CodeExamples.Advanced /// { /// public class IntegerArrayOperationExample /// { /// public void Main() /// { /// // Define an operator that squares its operand /// Func<![CDATA[<]]>int, int> square = (int operand) => operand * operand; /// /// // Define an array of operands /// int[] operands = new int[3] { 2, 4, 8 }; /// /// // Operate on it /// int[] results = IntegerArrayOperation.Operate(square, operands); /// /// // Show results /// for (int i = 0; i <![CDATA[<]]> results.Length; i++) /// { /// Console.WriteLine( /// "The result of squaring {0} is {1}.", /// operands[i], /// results[i]); /// } /// /// // Check that an operator cannot be null /// try /// { /// 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 /// { /// IntegerArrayOperation.Operate(square, 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. /// // /// // Cannot apply a null function: /// // Value cannot be null. /// // Parameter name: func /// // /// // Cannot apply a function to a null array: /// // Value cannot be null. /// // Parameter name: operands /// </code> /// </para> /// </example> public static int[] Operate(Func <int, int> func, int[] operands) { if (func == null) { throw new ArgumentNullException(nameof(func)); } if (operands == null) { throw new ArgumentNullException(nameof(operands)); } int[] result = new int[operands.Length]; for (int i = 0; i < result.Length; i++) { result[i] = IntegerOperation.Operate(func, operands[i]); } return(result); }