📄 chapter 5.mht
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: Delegates and Events - C# Nuts and Bolt - Beta2
Date: Wed, 29 Aug 2007 13:28:33 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0037_01C7EA40.7F6A7C00"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138
This is a multi-part message in MIME format.
------=_NextPart_000_0037_01C7EA40.7F6A7C00
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.vijaymukhi.com/documents/books/csadv/chap5.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns=3D"http://www.w3.org/TR/REC-html40" xmlns:v =3D=20
"urn:schemas-microsoft-com:vml" xmlns:o =3D=20
"urn:schemas-microsoft-com:office:office" xmlns:w =3D=20
"urn:schemas-microsoft-com:office:word"><HEAD><TITLE>Delegates and =
Events - C# Nuts and Bolt - Beta2</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3DWord.Document name=3DProgId>
<META content=3D"MSHTML 6.00.2900.3157" name=3DGENERATOR>
<META content=3D"Microsoft Word 9" name=3DOriginator><LINK=20
href=3D"./Chap5_files/filelist.xml" rel=3DFile-List><LINK=20
href=3D"./Chap5_files/editdata.mso" rel=3DEdit-Time-Data><!--[if !mso]>
<STYLE>v\:* {
BEHAVIOR: url(#default#VML)
}
o\:* {
BEHAVIOR: url(#default#VML)
}
w\:* {
BEHAVIOR: url(#default#VML)
}
.shape {
BEHAVIOR: url(#default#VML)
}
</STYLE>
<![endif]--><!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:Author>Vijay Mukhi</o:Author>
<o:LastAuthor>Vijay Mukhi</o:LastAuthor>
<o:Revision>1</o:Revision>
<o:TotalTime>1</o:TotalTime>
<o:Created>2001-09-27T12:14:00Z</o:Created>
<o:LastSaved>2001-09-27T12:15:00Z</o:LastSaved>
<o:Pages>22</o:Pages>
<o:Words>4556</o:Words>
<o:Characters>25974</o:Characters>
<o:Company>V.M.C.I.</o:Company>
<o:Lines>216</o:Lines>
<o:Paragraphs>51</o:Paragraphs>
<o:CharactersWithSpaces>31897</o:CharactersWithSpaces>
<o:Version>9.2720</o:Version>
</o:DocumentProperties>
</xml><![endif]-->
<STYLE>@font-face {
font-family: Tahoma;
}
@font-face {
font-family: Trebuchet MS;
}
@font-face {
font-family: Bookman Old Style;
}
@font-face {
font-family: Garamond;
}
@page Section1 {size: 8.5in 11.0in; margin: 1.0in 1.25in 1.0in 1.25in; =
mso-header-margin: .5in; mso-footer-margin: .5in; mso-paper-source: 0; }
P.MsoNormal {
FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman"; =
mso-style-parent: ""; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"
}
LI.MsoNormal {
FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman"; =
mso-style-parent: ""; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"
}
DIV.MsoNormal {
FONT-SIZE: 12pt; MARGIN: 0in 0in 0pt; FONT-FAMILY: "Times New Roman"; =
mso-style-parent: ""; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"
}
P.Cprg {
FONT-SIZE: 9.5pt; MARGIN: 0in 0in 0pt 0.5in; FONT-FAMILY: "Trebuchet =
MS"; mso-style-parent: "C#base"; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"; mso-style-name: "C#prg"; =
mso-layout-grid-align: none; mso-bidi-font-family: "Times New Roman"
}
LI.Cprg {
FONT-SIZE: 9.5pt; MARGIN: 0in 0in 0pt 0.5in; FONT-FAMILY: "Trebuchet =
MS"; mso-style-parent: "C#base"; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"; mso-style-name: "C#prg"; =
mso-layout-grid-align: none; mso-bidi-font-family: "Times New Roman"
}
DIV.Cprg {
FONT-SIZE: 9.5pt; MARGIN: 0in 0in 0pt 0.5in; FONT-FAMILY: "Trebuchet =
MS"; mso-style-parent: "C#base"; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"; mso-style-name: "C#prg"; =
mso-layout-grid-align: none; mso-bidi-font-family: "Times New Roman"
}
P.Coutput {
FONT-SIZE: 9.5pt; MARGIN: 0in 0in 0pt 0.5in; FONT-FAMILY: "Trebuchet =
MS"; mso-style-parent: "C#base"; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"; mso-style-name: "C#output"; =
mso-layout-grid-align: none; mso-bidi-font-family: "Times New Roman"
}
LI.Coutput {
FONT-SIZE: 9.5pt; MARGIN: 0in 0in 0pt 0.5in; FONT-FAMILY: "Trebuchet =
MS"; mso-style-parent: "C#base"; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"; mso-style-name: "C#output"; =
mso-layout-grid-align: none; mso-bidi-font-family: "Times New Roman"
}
DIV.Coutput {
FONT-SIZE: 9.5pt; MARGIN: 0in 0in 0pt 0.5in; FONT-FAMILY: "Trebuchet =
MS"; mso-style-parent: "C#base"; mso-pagination: widow-orphan; =
mso-fareast-font-family: "Times New Roman"; mso-style-name: "C#output"; =
mso-layout-grid-align: none; mso-bidi-font-family: "Times New Roman"
}
P.Cbase {
FONT-SIZE: 10pt; MARGIN: 0in 0in 0pt; COLOR: black; FONT-FAMILY: =
"Bookman Old Style"; TEXT-ALIGN: justify; mso-style-parent: ""; =
mso-pagination: widow-orphan; mso-fareast-font-family: "Times New =
Roman"; mso-style-name: "C#base"; mso-layout-grid-align: none; =
mso-bidi-font-family: "Times New Roman"
}
LI.Cbase {
FONT-SIZE: 10pt; MARGIN: 0in 0in 0pt; COLOR: black; FONT-FAMILY: =
"Bookman Old Style"; TEXT-ALIGN: justify; mso-style-parent: ""; =
mso-pagination: widow-orphan; mso-fareast-font-family: "Times New =
Roman"; mso-style-name: "C#base"; mso-layout-grid-align: none; =
mso-bidi-font-family: "Times New Roman"
}
DIV.Cbase {
FONT-SIZE: 10pt; MARGIN: 0in 0in 0pt; COLOR: black; FONT-FAMILY: =
"Bookman Old Style"; TEXT-ALIGN: justify; mso-style-parent: ""; =
mso-pagination: widow-orphan; mso-fareast-font-family: "Times New =
Roman"; mso-style-name: "C#base"; mso-layout-grid-align: none; =
mso-bidi-font-family: "Times New Roman"
}
DIV.Section1 {
page: Section1
}
</STYLE>
</HEAD>
<BODY lang=3DEN-US style=3D"tab-interval: .5in">
<DIV class=3DSection1>
<P class=3DMsoNormal><IMG id=3D_x0000_i1025 height=3D48=20
src=3D"http://www.vijaymukhi.com/documents/books/csadv/bpb.jpg" =
width=3D64></P>
<P class=3DMsoNormal><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DMsoNormal><SPAN=20
style=3D"FONT-SIZE: 15pt; COLOR: black; FONT-FAMILY: Garamond">Chapter=20
5</SPAN></P>
<P class=3DMsoNormal><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DMsoNormal><SPAN=20
style=3D"FONT-SIZE: 26pt; COLOR: black; FONT-FAMILY: Garamond">Delegates =
and=20
Events</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">A delegate is =
extremely important=20
for C# as it is one of the four entities that can be placed in a =
namespace. This=20
makes it shareable among classes. Delegates are fully object oriented as =
they=20
entirely enclose or encapsulate an object instance and a method. A =
delegate=20
defines a class and extends System.Delegate. It can call any function as =
long as=20
the methods signature matches the delegates. This makes delegates ideal =
for=20
anonymous invocation. The methods signature only includes the return =
type and=20
the parameter list. If two delegates have the same parameter and return =
type,=20
that is, they share the same signature, we consider them as different =
delegates.=20
This chapter will feature these issues using simple programs.=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 void 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("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">a is an object that =
looks like=20
aa. a.abc will call the abc function from this class. Earlier on, we =
specified=20
that new must be followed by the class. We stand corrected as usual =
here.=20
Likewise, we can also use a delegate name after new. A delegate is like =
a=20
property or an indexer. It is a first class member of a class. It looks =
like a=20
function, but has the keyword delegate in front of it along with the =
return type=20
of void.<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">When we use new to =
instantiate a=20
delegate, we pass it one parameter. This happens to be the name of a =
function,=20
namely, pqr1. This function is created in the same class, that is, aa, =
it takes=20
no parameters and returns a void. The keyword new obviously creates an =
object=20
that looks like a delegate as it was given a delegate as a datatype. d =
is now an=20
object that looks like pqr and stores the delegate object of pqr1. Then =
we use=20
the function invocation syntax and write d(). To our utter surprise, we =
have=20
actually executed the function pqr1 without writing pqr1().=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">The reasoning here is =
that d is a=20
delegate object for function pqr1 as we have passed it as a parameter at =
the=20
time of creation. Calling d calls function pqr1 instead. This is one =
level of=20
abstraction thus helping us write complicated =
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 =
()<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(100);<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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -