📄 chapter 5.mht
字号:
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">The above program =
takes the=20
abstract behavior of a delegate a step further. Nothing on earth stops =
you from=20
executing a delegate through a loop construct. What we are repeating =
over and=20
over again is that a delegate passed as a parameter to a function, does =
not know=20
the name of the function it is going to execute at the time of =
compilation. It=20
will only receive this information at the time of execution i.e. at run=20
time.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz {<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>aa a =3D new aa();<o:p></o:p></P>
<P class=3DCprg>a.abc();<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public delegate void pqr();<o:p></o:p></P>
<P class=3DCprg>public class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>void<SPAN style=3D"mso-spacerun: yes"> </SPAN>pqr1 =
()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("pqr1"); <o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>pqr d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D =
new=20
pqr(pqr1);<o:p></o:p></P>
<P class=3DCprg>d();<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>pqr1<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">pqr is a delegate as =
it defines a=20
class that extends System.Delegate.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public delegate void pqr();<o:p></o:p></P>
<P class=3DCprg>public class aa : pqr<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Compilation Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(2,14): error CS0509: 'aa' : cannot inherit from =
sealed=20
class 'pqr'<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">A delegate internally =
is=20
represented by a class with the same name. In the above case, the class =
pqr is=20
implicitly sealed. A sealed class cannot be used in a derivation. A long =
time=20
ago we had explained to you that there were four classes you could not =
derive=20
from. If memory serves you well, one of them was System.Delegate and it =
is an=20
abstract class. All delegates derive from it automatically. We can =
access all=20
the members of System.Delegate in the normal way. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>aa a =3D new aa();<o:p></o:p></P>
<P class=3DCprg>a.abc();<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public delegate void pqr();<o:p></o:p></P>
<P class=3DCprg>public class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>void<SPAN style=3D"mso-spacerun: yes"> </SPAN>pqr1 =
()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("pqr1"); <o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>pqr d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D =
new=20
pqr(pqr1);<o:p></o:p></P>
<P class=3DCprg>d();<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(d.ToString());<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>pqr1<o:p></o:p></P>
<P class=3DCoutput>pqr<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">System.Delegate is =
also derived=20
from Object and hence using the syntax learnt earlier of member access, =
we can=20
call the ToString function of d.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public delegate void pqr();<o:p></o:p></P>
<P class=3DCprg>public class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(pqr.ToString());<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(6,26): error CS0120: An object reference is =
required for=20
the nonstatic field, method, or property =
'object.ToString()'<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">We cannot use the =
name of the=20
delegate pqr with function ToString as it is not static. d being an =
instance of=20
the class can be used and will give no errors.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Delegate types come =
in two sizes,=20
combinable and non-combinable. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>using System;<o:p></o:p></P>
<P class=3DCprg>delegate void ddd(string s);<o:p></o:p></P>
<P class=3DCprg>class zzz<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void a1(string s)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>Console.WriteLine(s + " a1");<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public static void a2(string s)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>Console.WriteLine(s + " a2");<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>ddd a, b, c, d;<o:p></o:p></P>
<P class=3DCprg>a =3D new ddd(a1);<o:p></o:p></P>
<P class=3DCprg>b =3D new ddd(a2);<o:p></o:p></P>
<P class=3DCprg>c =3D a + b; <o:p></o:p></P>
<P class=3DCprg>d =3D a - b; <o:p></o:p></P>
<P class=3DCprg>a("A");<o:p></o:p></P>
<P class=3DCprg>b("B");<o:p></o:p></P>
<P class=3DCprg>c("C");<o:p></o:p></P>
<P class=3DCprg>d("D");<o:p></o:p></P>
<P class=3DCprg>d =3D c - a;<o:p></o:p></P>
<P class=3DCprg>d("E");<o:p></o:p></P>
<P class=3DCprg>d =3D c - b;<o:p></o:p></P>
<P class=3DCprg>d("F");<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>A a1<o:p></o:p></P>
<P class=3DCoutput>B a2<o:p></o:p></P>
<P class=3DCoutput>C a1<o:p></o:p></P>
<P class=3DCoutput>C a2<o:p></o:p></P>
<P class=3DCoutput>D a1<o:p></o:p></P>
<P class=3DCoutput>E a2<o:p></o:p></P>
<P class=3DCoutput>F a1<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">This example proves =
the=20
importance of delegates in C# and they can be made extremely complex if =
you so=20
choose to. Objects a, b, c and d are delegate type objects. Object a and =
b=20
represent functions a1 and a2 respectively. Thus a("A") and b("B") call=20
functions a1 and a2.<SPAN style=3D"mso-spacerun: yes"> </SPAN>This =
is shown=20
above. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Object c represents =
the summation=20
of objects a and b. Obviously we cannot add two delegate types as they =
are not=20
numbers. The only rational explanation possible is that we want both the =
delegates to be executed. First, function a1 gets called and then a2. =
The object=20
d is initialized to a - b. This will eliminate functions contained in a2 =
from=20
a1. As we have no common function in a2, it makes no difference and =
function a1=20
is called. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">The next series of =
statements=20
explains the operations better. The object d is made equal to c-a. This =
will=20
subtract all the functions that delegate a represents from the object c. =
The=20
object c stands for functions a1 and a2, and as a represents function =
a1,=20
function a1 gets removed from d. Thus a2 is called. In the last case of =
c - b,=20
had we executed the object c, both functions a1 and a2 would get called. =
But as=20
we are subtracting b i.e. function a2, only a1 gets called.=20
<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">You can call as many =
functions=20
you want through delegates.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>aa a =3D new aa();<o:p></o:p></P>
<P class=3DCprg>a.abc();<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public delegate int pqr();<o:p></o:p></P>
<P class=3DCprg>public class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int pqr1 ()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("pqr1"); <o:p></o:p></P>
<P class=3DCprg>return 0;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>pqr d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D =
new=20
pqr(pqr1);<o:p></o:p></P>
<P class=3DCprg>pqr e<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D =
new=20
pqr(pqr1);<o:p></o:p></P>
<P class=3DCprg>pqr f =3D d + e;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">Two delegates can be =
combined=20
provided the return type of each is not void. As our newly defined =
delegate,=20
pqr, returns an int, we get no errors. <o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>a.cs</U><o:p></o:p></P>
<P class=3DCprg>public class zzz {<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>aa a =3D new aa();<o:p></o:p></P>
<P class=3DCprg>a.abc();<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public delegate int pqr(out int i);<o:p></o:p></P>
<P class=3DCprg>public class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int pqr1 (out int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("pqr1"); <o:p></o:p></P>
<P class=3DCprg>i =3D 10;<o:p></o:p></P>
<P class=3DCprg>return 0;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>pqr d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D =
new=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -