/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// SimpDlgt.cs -- Demonstrates a simple form of a delegate
//
// Compile this program using the following command line:
// C:>csc SimpDlgt.cs
using System;
namespace nsDelegate
{
// Declare the delegate. This actually creates a new class definition.
delegate double MathOp (double value);
public class SimpDlgt
{
static public void Main ()
{
// Declare an object of the delegate type.
MathOp DoMath;
// Create the delgate object using a method name.
DoMath = new MathOp (GetSquare);
// Execute the delegate. This actually calls the Invoke() method.
double result = DoMath (3.14159);
// Show the result.
Console.WriteLine (result);
// Assign another method to the delegate object
DoMath = new MathOp (GetSquareRoot);
// Call the delegate again.
result = DoMath (3.14159);
// Show the result.
Console.WriteLine (result);
}
// Return the square of the argument.
static double GetSquare (double val)
{
return (val * val);
}
// Return the square root of the argument.
static double GetSquareRoot (double val)
{
return (Math.Sqrt (val));
}
}
}
|