Delegate -- delegate

Source: Internet
Author: User
1 using System;
2 namespace uu
3 {
4 public class Delegates
5 {
6 public static void Main ()
7 {
8 long length;
9 long width;
10 long height;
11 Console. Write ("Enter the length :");
12 length = long. Parse (Console. ReadLine ());
13 Console. Write ("Enter the width ");
14 width = long. Parse (Console. ReadLine ());
15 Console. Write ("Enter the height ");
16 height = long. Parse (Console. ReadLine ());
17 GGResult myGetResult = new GGResult (height, length, width );
18 Measure myMeasure = new Measure ();
19 GGResult. MyDelegate myGRdelegate;
20 if (length = width)
21 {
22 myGRdelegate = new GGResult. MyDelegate (myMeasure. MeasureSquare); // a parameter is prompted.
23}
24 else
25 {
26 myGRdelegate = new GGResult. MyDelegate (myMeasure. MeasureRectangle); // The system prompts you to set a parameter.
27}
28 myGetResult. Calc (myGRdelegate );
29 Console. ReadLine ();
30}
31}
32 // GGResult class
33 public class GGResult
34 {
35 long height;
36 long width;
37 long length;
38 public GGResult (long height, long width, long length)
39 {
40 this. height = height;
41 this. width = width;
42 this. length = length;
43}
44 public delegate double MyDelegate (long length, long width); // declare the delegate
45 public void Calc (MyDelegate pmydelegate)
46 {
47 double volume;
48 volume = pmydelegate (length, width) * height;
49 Console. WriteLine ("if the height is {0}, the volume is {1}", height, volume );
50}
51}
52 // Measure class
53 public class Measure
54 {
55 public double MeasureSquare (long length, long width)
56 {
57 double squareLength;
58 double squareSurface;
59 squareLength = 4 * length;
60 squareSurface = Math. Pow (length, 2 );
61 Console. WriteLine ("the perimeter of the square where the width is {0} and the width is {1} is {2}, and the area is {3}", length, width, squareLength, squareSurface );
62 return squareSurface;
63}
64 public double MeasureRectangle (long length, long width)
65 {
66 double RectangleLength;
67 double RectangleSurface;
68 RectangleLength = 2 * (length + width );
69 RectangleSurface = length * width;
70 Console. WriteLine ("the circumference of a rectangle with a {0} width of {1} is {2}, and the surface is {3}:", length, width, RectangleLength, RectangleSurface );
71 return RectangleSurface;
72}
73}
74}

Delegate is equivalent to a container, which is used to store functions or encapsulate functions. This container can be regarded as a function, and this function does not
To implement the function, you only need to return the same type and parameter as the function encapsulated by Yao. Then you can encapsulate this function.
For example:
1. declared a double-type delegate myDelegate with the parameter (long length, long width); then this delegate can only encapsulate the returned value and is double, and
The function whose parameter is long.
2. In the Measure class, there are two methods: MeasureSquare and MeasureRectangle. Both of these functions have reverse return values of the double type, and the parameter is also long.
Type. Therefore, both functions can be divided into delegates.
3. As mentioned above, delegation is equivalent to a function. Since it is a function, it can have parameters and return values (of course, it must be consistent with the encapsulated function ).
1. There is a method Calc in the GGResult class; its parameter is a delegate type parameter. In fact, it calls the delegate encapsulated method and initializes it according to the condition.
The method encapsulated by myDelegate is then transmitted to Cale in the form of parameters, and then called in Calc.
There is nothing to see about this program. In fact, it is not. It seems that there is no problem to simply look at it, but after careful consideration, the problem will come out.
MyDelegate = new GGResult. MyDelegate (myMeasure. MeasureSquare );
MyDelegate = new GGResult. MyDelegate (myMeasure. MeasureRectangle );
The two statements indicate that the delegate is used to encapsulate the function, not to execute the statement. So when will this delegate be executed?
In myGetResult. Calc (myGRdelegate);, because there is such a sentence in Calc
Volume = pmydelegate (length, width) * height;
Therefore, when pmydelegate is executed, the methods in one of the above delegates are started.
So where does the parameter come from? The answer is: Initialize the parameter to the GGResult constructor when initializing GGResult, and then call pmydelegate.
It is called directly.


Note: When declaring a function in a class, if the function does not have the static keyword, it can be referenced through an instance of the class. If there is a static keyword, you can only use
Class Name.
Class initialization:
In GGResult, a delegate class MyDelegate must be added during initialization.
Both GGResult. MyDelegate Instance name = new GGResult. MyDelegate ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.