Delegates and Arguments |
|
A Delegate That Takes One of More Arguments |
If you want to associate a method that takes arguments to a
delegate, when declaring the delegate, provide the necessary argument(s) in its
parentheses. Here is an example of a delegate that takes two arguments (and
returns a value):
When defining the associated method, besides returning the
same type of value if not void, make sure that the method takes the same number
of arguments. Here is an example: |
using System; delegate void dlgSimple(); delegate double Addition(double x, double y); class Exercise { private static void Welcome() { Console.WriteLine("Welcome to the Wonderful World of C# Programming!"); } private static double Plus(double a, double b) { return a + b; } static int Main() { dlgSimple Announce = new dlgSimple(Welcome); Announce(); return 0; } } Once again, to associate the method, declare a variable of the type of delegate and pass the name of the method to the constructor of the delegate. Here is an example: Addition Add = new Addition(Plus); Notice that only the name of the method is passed to the delegate. To actually use the delegate, when calling it, in its parentheses, provide a value for the argument(s) conform to the type specified when declaring the delegate. Here is an example: using System; delegate void dlgSimple(); delegate double Addition(double x, double y); class Exercise { private static void Welcome() { Console.WriteLine("Welcome to the Wonderful World of C# Programming!"); } private static double Plus(double a, double b) { return a + b; } static int Main() { double Value1 = 248.66, Value2 = 50.26, Result; dlgSimple Announce = new dlgSimple(Welcome); Addition Add = new Addition(Plus); Result = Add(Value1, Value2); Announce(); Console.WriteLine("\n{0} + {1} = {2}", Value1, Value2, Result); return 0; } } This would produce: Welcome to the Wonderful World of C# Programming! 248.66 + 50.26 = 298.92
Using delegates, one method can be indirectly passed as argument to another method. To proceed, first declare the necessary delegate. Here is a example of such a delegate: using System; namespace GeometricFormulas { public delegate double Squared(double x); public class Circle { private double _radius; public double Radius { get { return _radius; } set { _radius = value; } } } } A delegate can be passed as argument to a method. Such an argument would be used as if it were a method itself. This means that, when accessed in the body of the method, the name of the delegate must be accompanied by parentheses and if the delegate takes an argument or argument, the argument(s) must be provided in the parentheses of the called delegate. Here is an example: using System; namespace GeometricFormulas { public delegate double Squared(double x); public class Circle { private double _radius; public double Radius { get { return _radius; } set { _radius = value; } } public double Area(Squared sqd) { return sqd(_radius) * Math.PI; } } } After declaring a delegate, remember to define a method that implements the needed behavior of that delegate. Here is an example: using System; namespace GeometricFormulas { public delegate double Squared(double x); public class Circle { private double _radius; public static double ValueTimesValue(double Value) { return Value * Value; } } } You can also define the associated method in another class, not necessarily in the class where the delegate would be needed. Once the method that implements the delegate is known, you can use the delegate as you see fit. To do that, you can declare a variable of the type of that delegate and pass the implementing method to its constructor. Here is an example: using System; namespace GeometricFormulas { public delegate double Squared(double x); public class Circle { private double _radius; public static double ValueTimesValue(double Value) { return Value * Value; } public double Area(Squared sqd) { return sqd(_radius) * Math.PI; } public void CircleCharacteristics() { Squared Sq = new Squared(ValueTimesValue); } } } This declaration gives life to the delegate and can then be used as we have proceed with delegates so far. Here is an example: using System; namespace GeometricFormulas { public delegate double Twice(double x); public delegate double Squared(double x); public class Circle { private double _radius; public static double TwoTimes(double Value) { return Value * 2; } public static double ValueTimesValue(double Value) { return Value * Value; } public double Radius { get { return _radius; } set { _radius = value; } } public Circle() { _radius = 0; } public Circle(double r) { _radius = r; } public double Diameter(double rad) { return rad * 2; } public double Circumference(Twice FDiam) { double Circf; Circf = FDiam(_radius); return Circf * Math.PI; } public double Area(Squared sqd) { return sqd(_radius) * Math.PI; } public void CircleCharacteristics() { Squared Sq = new Squared(ValueTimesValue); Twice Tw = new Twice(TwoTimes); Console.WriteLine("Circle Characteristics"); Console.WriteLine("Radius: {0}", Radius); Console.WriteLine("Diameter: {0}", Diameter(_radius)); Console.WriteLine("Circumference: {0}", Circumference(Tw)); Console.WriteLine("Area: {0}\n", Area(Sq)); } } class Exercise { static int Main() { Circle Round = new Circle(25.58); Round.CircleCharacteristics(); return 0; } } } This would produce: Circle Characteristics Radius: 25.58 Diameter: 51.16 Circumference: 160.723880157654 Area: 2055.65842721639 |
|
||
Previous | Copyright © 2006-2007 FunctionX, Inc. | Home |
|