📄 chapter 1.mht
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: Concepts Revisited - C# Nuts and Bolt - Beta2
Date: Wed, 29 Aug 2007 13:27:37 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0023_01C7EA40.5DE0F960"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138
This is a multi-part message in MIME format.
------=_NextPart_000_0023_01C7EA40.5DE0F960
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.vijaymukhi.com/documents/books/csadv/chap1.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>Concepts Revisited =
- 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"./Chap1_files/filelist.xml" rel=3DFile-List><LINK=20
href=3D"./Chap1_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>3</o:TotalTime>
<o:Created>2001-09-27T11:52:00Z</o:Created>
<o:LastSaved>2001-09-27T11:55:00Z</o:LastSaved>
<o:Pages>34</o:Pages>
<o:Words>7861</o:Words>
<o:Characters>44809</o:Characters>
<o:Company>V.M.C.I.</o:Company>
<o:Lines>373</o:Lines>
<o:Paragraphs>89</o:Paragraphs>
<o:CharactersWithSpaces>55028</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
1</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">Concepts=20
Revisited</SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: Tahoma">Entry=20
Point</SPAN></B><SPAN style=3D"COLOR: windowtext"><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 first function to =
be called=20
in any C# program is Main. Obviously someone at Microsoft tossed a coin =
and as=20
it fell 'heads', they chose Main. Had it been 'tails', one wonders what =
the=20
first function would have been called. As everything starts from Main, =
it is=20
also called the entry point of the program. It is the fountainhead of =
all=20
knowledge. Incidentally, the C programming language also calls Main as =
its first=20
function for all C-language programs.<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 can have as many =
as four=20
different ways to declare Main in our program. They are as=20
follows:<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>static void Main() {...}<o:p></o:p></P>
<P class=3DCprg>static int Main() {...}<o:p></o:p></P>
<P class=3DCprg>static void Main(string[] a) {...}<o:p></o:p></P>
<P class=3DCprg>static int Main(string[] args) {...}<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">The operating system =
calls Main=20
and waits for it to return a value. This value denotes the success or =
failure of=20
the program.<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">Main can either =
return a number=20
or result in no return value, that is, void. If it returns an int, by=20
convention, a value of zero means success and any other value indicates =
an=20
error. No international authority can standardize the error numbers, it =
largely=20
depends on the programmer himself. <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">Copy as a command =
takes two=20
parameters; the source and destination files. Similarly, a C# program =
can also=20
accept command line parameters at runtime. The executable<SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>a.exe can be entered=20
as<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>>a one two three<o:p></o:p></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>class zzz<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main(string[] a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int i;<o:p></o:p></P>
<P class=3DCprg>for ( i=3D0; i< a.Length; i++)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(a[i]);<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>one<o:p></o:p></P>
<P class=3DCoutput>two<o:p></o:p></P>
<P class=3DCoutput>three<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">one, two, three are =
called=20
command line parameters. The program accepts them in an array of =
strings. As the=20
array is a parameter to the function, we are free to decide on its name. =
Every=20
array has a member called Length, which tells us the size of the array. =
In our=20
case, it is three. Thus, a[0] will contain the first word one and not =
the name=20
of the program, a[1] will contain two and a[3] - three. Main now behaves =
like=20
any other function. What we do next with the command line arguments =
depends=20
entirely upon us. <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>class zzz <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static void Main(string[] a)<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 static int Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>} <o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P=20
class=3DCoutput><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(3,20): error CS0017: Program 'a.exe' has more =
than one=20
entry point defined: 'zzz.Main(string[])'<o:p></o:p></P>
<P class=3DCoutput>a.cs(6,19): error CS0017: Program 'a.exe' has more =
than one=20
entry point defined: 'zzz.Main()'<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">You can have one and =
only one=20
function called Main in any C# program. Even though you can call it with =
different parameters, with the name changing, Main as a function must be =
given=20
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -