/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Delegate.cs - Demonstrates the minimum implementation of a delegate // Compile this program with the following command line: // C:>csc Delegate.cs // namespace nsDelegate { using System; public class Delegate21 { // Declare the delegate type public delegate double MathHandler (double val); // Declare the variable that will hold the delegate public MathHandler DoMath; static public void Main () { double val = 31.2; double result; Delegate21 main = new Delegate21(); // Create the first delegate main.DoMath = new Delegate21.MathHandler (main.Square); // Call the function through the delegate result = main.DoMath (val); Console.WriteLine ("The square of {0,0:F1} is {1,0:F3}", val, result); // Create the second delegate main.DoMath = new Delegate21.MathHandler (main.SquareRoot); // Call the function through the delegate result = main.DoMath (val); Console.WriteLine ("The square root of {0,0:F1} is {1,0:F3}", val, result); } public double Square (double val) { return (val * val); } public double SquareRoot (double val) { return (Math.Sqrt (val)); } } }