Functions as first-class citizens
In Chapter 8, Advanced Topics, we learned about delegates and events. A delegate looks like a function but is a type that holds references to functions whose signatures match the definition of the delegate. Delegate instances can be passed as objects for function arguments. Let's look at an example where we have a delegate that takes two int
parameters and returns an int
value:
public delegate int Combine(int a, int b);
We then have different functions, such as Add()
, which adds two integers and returns the sum, Sub()
, which subtracts two integers and returns their difference, or Mul()
, which multiplies two integers and returns their product. Their signature matches the delegate, so an instance of the Combine
delegate can hold references to all these functions. These functions are shown as follows:
class Math { Â Â Â Â public static int Add(int a, int b) { return a + b; } Â Â Â Â public static int Sub(int...