📄 chapter 5.mht
字号:
<P class=3DCoutput>a.cs(19,1): error CS1593: Delegate 'pqr' does not =
take '1'=20
arguments<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">C# does not let you =
do whatever=20
your heart desires. The delegate object d that looks like a pqr is now =
passed=20
one parameter, that is, 100. As d stands for the function pqr1 and pqr1 =
does not=20
accept an int as a parameter, an error is shown. This proves that C# =
does strict=20
type checking on the code.<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 class aa<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>void<SPAN style=3D"mso-spacerun: yes"> </SPAN>pqr1 =
(int=20
d)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("pqr"); <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>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(18,18): error CS0123: Method 'aa.pqr1(int)' does =
not match=20
delegate 'void aa.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">In our infinite =
wisdom, we=20
thought that the above error will vanish if we added an int as a =
parameter to=20
our function pqr1. C# however tends to disagree and now gives us =
different error=20
message.<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 class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public delegate int pqr(int ddd);<o:p></o:p></P>
<P class=3DCprg>int pqr1 (int dd)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return dd * 2;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>int pqr2 (int dd)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return dd * 10;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>pqr d;<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>d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D new =
pqr(pqr1);<o:p></o:p></P>
<P class=3DCprg>int<SPAN style=3D"mso-spacerun: yes"> </SPAN>d1 =
=3D=20
d(42);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(d1); <o:p></o:p></P>
<P class=3DCprg>d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D new =
pqr(pqr2);<o:p></o:p></P>
<P class=3DCprg>int<SPAN style=3D"mso-spacerun: yes"> </SPAN>d2 =
=3D=20
d(42);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(d2); <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>84<o:p></o:p></P>
<P class=3DCoutput>420<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">To eliminate the =
error message,=20
we added an int parameter to the delegate pqr. This informs the C# =
compiler that=20
whoever calls the delegate pqr, will hand it one parameter. The delegate =
now=20
returns an int unlike the earlier code where we returned void. Then C# =
checks=20
whether the function name given at the time of creation of the delegate =
accepts=20
one parameter and returns an int. Finally, when we run the function =
through the=20
delegate, a last check is made to see that the right parameters are =
given.=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">We have also added a =
tweeze of=20
lime to our program. We have created one more delegate type but now it =
is with=20
another function name as its parameter. The code remains the same. We =
keep=20
executing the delegate as d(42), but the function to be called changes =
each=20
time. This offers us greater flexibility in writing dynamic code.=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=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 class aa<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>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("pqr"); <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>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(18,18): error CS0123: Method 'aa.pqr1()' does =
not match=20
delegate 'int aa.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">C# is very fussy =
about a=20
delegate. The return value of the delegate in the definition says that =
it will=20
return an int but the function pqr1 returns a void=20
instead.<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 class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public delegate int pqr(int ddd);<o:p></o:p></P>
<P class=3DCprg>int pqr1 (int dd)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return dd * 2;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>int pqr2 (int dd)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return dd * 10;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void xyz( pqr a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int f =3D a(10);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(f); <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>xyz(d);<o:p></o:p></P>
<P class=3DCprg>d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D new =
pqr(pqr2);<o:p></o:p></P>
<P class=3DCprg>xyz(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>20<o:p></o:p></P>
<P class=3DCoutput>100<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 function =
demonstrates a=20
simple fact that a delegate is like a class. We can give objects that =
look like=20
classes as parameters to functions. Int, long etc are classes in C#. A =
delegate=20
being a data type can be passed as a parameter to a function.=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">In the first and =
second=20
invocation of function xyz we are passing the same delegate type of d. =
The first=20
time it stands for the function pqr and the second time function pqr2. =
Using the=20
object a, we are executing a different function each time. The code of =
the=20
function xyz remains the same. We are indirectly giving it a different =
function=20
each time. The code of function xyz does not know nor care about the =
function it=20
is calling. <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">Writing generic code =
like the=20
above, helps isolate the code implementation from its execution. More on =
these=20
abstract issues in a short while.<SPAN style=3D"mso-spacerun: =
yes"> =20
</SPAN><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 class aa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public delegate int pqr(int ddd);<o:p></o:p></P>
<P class=3DCprg>int pqr1 (int dd)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return dd * 2;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>int pqr2 (int dd)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return dd * 10;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void xyz( pqr a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>for ( int i =3D 1; i<=3D3 ; i++)<o:p></o:p></P>
<P class=3DCprg>System.Console.Write(a(i) + " "); <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>xyz(d);<o:p></o:p></P>
<P class=3DCprg>d<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D new =
pqr(pqr2);<o:p></o:p></P>
<P class=3DCprg>xyz(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>2 4 6 10 20 30<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -