⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 3.mht

📁 C# Nuts and Bolt是学习C#的极好教程
💻 MHT
📖 第 1 页 / 共 4 页
字号:
From: <Saved by Microsoft Internet Explorer 5>
Subject: Enums - C# Nuts and Bolt - Beta2
Date: Wed, 29 Aug 2007 13:28:09 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
	type="text/html";
	boundary="----=_NextPart_000_002D_01C7EA40.70F15860"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138

This is a multi-part message in MIME format.

------=_NextPart_000_002D_01C7EA40.70F15860
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.vijaymukhi.com/documents/books/csadv/chap3.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>Enums - 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"./Chap3_files/filelist.xml" rel=3DFile-List><LINK=20
href=3D"./Chap3_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-27T11:57:00Z</o:Created>
  <o:LastSaved>2001-09-27T11:58:00Z</o:LastSaved>
  <o:Pages>10</o:Pages>
  <o:Words>1748</o:Words>
  <o:Characters>9969</o:Characters>
  <o:Company>V.M.C.I.</o:Company>
  <o:Lines>83</o:Lines>
  <o:Paragraphs>19</o:Paragraphs>
  <o:CharactersWithSpaces>12242</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]>&nbsp;<o:p></o:p></P>
<P class=3DMsoNormal><SPAN=20
style=3D"FONT-SIZE: 15pt; COLOR: black; FONT-FAMILY: Garamond">Chapter=20
3</SPAN></P>
<P class=3DMsoNormal><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DMsoNormal><SPAN=20
style=3D"FONT-SIZE: 26pt; COLOR: black; FONT-FAMILY: =
Garamond">Enums</SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">An enum is at the =
same level as a=20
class, struct and interface. It performs a similar job of creating a new =
data=20
type.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<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()<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 class=3DCprg>enum yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>a1,black,hell<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">We have created a new =
datatype=20
yyy using enum. It has three values, namely, a1, black and hell. The =
words we=20
write within an enum can be anything, it is left entirely on=20
us.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<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()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine((int)yyy.black);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>enum yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>a1,black,hell<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>1<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">An enum can be =
considered as a=20
static object, hence black can be referred to as yyy.black. In the =
WriteLine=20
method, when we cast yyy.black to int, the output returned is an integer =
value=20
of 1. Without any cast, the output is displayed as =
black.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">In a sense, it =
behaves like an=20
array where a1 has a value of zero, black has a value of 1 and hell 2. =
Wherever=20
we refer to yyy.black, it is like writing the number 1</SPAN><SPAN=20
style=3D"COLOR: blue">.</SPAN> At one level an enum is a word that =
stands for a=20
constant number. An enum type is called a distinct type with named=20
constants.<SPAN style=3D"COLOR: windowtext"><o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<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>public static void Main()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P =
class=3DCprg>System.Console.WriteLine((byte)yyy.black);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>enum yyy : byte<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>a1,black,hell<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>1<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">In the earlier =
example on enum,=20
the data type for the members was int as we did not specify the =
underlying data=20
type. Here we are specifying byte as the underlying data type.=20
<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">The valid underlying =
types are=20
byte, sbyte, short, ushort, int, uint, long or ulong respectively. A =
char cannot=20
be the underlying type for an enum data object as a char stores Unicode=20
characters and not numbers.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<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()<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 class=3DCprg>enum yyy <o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>a1,black,hell<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>enum xxx : yyy<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]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(12,1): error CS1008: Type byte, sbyte, short, =
ushort, int,=20
uint, long, or ulong expected<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -