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

📄 chapter 4.mht

📁 C# Nuts and Bolt是学习C#的极好教程
💻 MHT
📖 第 1 页 / 共 5 页
字号:
difference between a value and a reference type. While copying in a =
value type,=20
a variable value is copied by creating a separate copy of the variable =
in=20
memory. In a reference type, only a reference to the object is copied =
and not=20
the actual contents. <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">All value types have =
a=20
constructor that is called at the time of creation of the object. Thus, =
when we=20
write int i, this free public parameter-less constructor is called. This =
is also=20
the default constructor. For all the simple types, this default =
constructor=20
initializes the variable to zero or in the case of floating point types, =
to be=20
specific, 0.0. Enums are constants and so their values are also =
initialized to=20
zero. In the case of a struct, all value types are made zero and =
reference type=20
are initialized to null. <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>aaa 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>struct aaa<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public int i;<o:p></o:p></P>
<P class=3DCprg>public string j;<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 Warning</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(10,12): warning CS0649: Field 'aaa.i' is never =
assigned=20
to, and will always have its default value 0<o:p></o:p></P>
<P class=3DCoutput>a.cs(11,15): warning CS0649: Field 'aaa.j' is never =
assigned=20
to, and will always have its default value null<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">Need we add anything =
more? The=20
compiler after a very long time agrees with us to no end and repeats =
what we=20
told you in the earlier paragraph. If error messages and warnings were =
more=20
verbose, would you want such weighty books? We surely would be out of a=20
job.<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>int i;<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>int j =3D new int();<o:p></o:p></P>
<P class=3DCprg>zzz a =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(a.i+ " " + j);<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 Warning</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(3,5): warning CS0649: Field 'zzz.i' is never =
assigned to,=20
and will always have its default value 0<o:p></o:p></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>0 0<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">Whether we write int =
i or new=20
int(), the effect is the same. An int object/variable<SPAN=20
style=3D"mso-spacerun: yes">&nbsp; </SPAN>is created and the constructor =
is=20
called. This initializes the variable to zero. Thus, use of new is =
optional for=20
the basic types. We thank the compiler for such small=20
mercies.<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>int j =3D new int(10);<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(5,9): error CS0143: The type 'int' has no =
constructors=20
defined<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 peeked into the =
structure=20
System.Int32 to locate for a constructor that accepts one parameter. But =
to our=20
dismay, we never found it. The designers chose not to implement such a=20
constructor, a decision they made. And, it is at our discretion to =
criticize or=20
not on their decision. Their volition is a mystery to all of us. Is =
anyone out=20
there in outer space listening? <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">Also, as every =
structure has a=20
constructor with no parameters, which initializes every member to its =
default=20
value, we are obviously not permitted to create our own constructor with =
no=20
parameters. This would create a piquant situation where we would end up =
having=20
two constructors with no parameters. Whenever the compiler gives us =
something=20
free, we should be highly indebted to it and never try and repeat the =
same code.=20
Duplicates are a problem wherever you go.<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">The simple data types =
are a=20
little different from the structure types not only because the compiler=20
considers them to be reserved words in the language but also they are =
treated in=20
a slightly different way. <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"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">First and foremost, a =
simple type=20
can be initialized by using a literal 123, e.g. int i =3D 123. Literals =
cannot be=20
used with user-defined struct types. They can be used only with simple =
types and=20
the value types. The value of a struct can only be determined at run =
time as the=20
constructor initializes the members. Thus, a constant expression can be =
created=20
with simple value types. Score two for the simple value types.=20
<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">At the same level, a =
structure=20
type cannot be a constant. To solve this problem, the compiler =
introduced static=20
readonly fields. They have the same effect as constants. Score + point =
for a=20
value field. Lastly, a value type can be converted into other value =
types for=20
matching a parameter in a user defined conversion operator. This is not =
possible=20
for other user defined conversion parameters involving other structure =
types.=20
<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><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: =
Tahoma">Integral=20
Types</SPAN></B><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=3DCbase><SPAN style=3D"COLOR: windowtext">Warning. Lots of =
tiresome=20
information ahead and less of Code. <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">We have nine integral =
types in=20
all. They amount of memory they require and the range of values they can =
handle=20
are as follows. <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=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The sbyte=20
type represents a signed 8-bit integers with values between -128 and =
127.=20
<o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The byte=20
type represents unsigned 8-bit integers with values between 0 and 255.=20
<o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The short=20
type represents signed 16-bit integers with values between -32768 and =
32767.=20
<o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The ushort=20
type represents unsigned 16-bit integers with values between 0 and =
65535.=20
<o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The int=20
type represents signed 32-bit integers with values between -2147483648 =
and=20
2147483647. <o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The uint=20
type represents unsigned 32-bit integers with values between 0 and =
4294967295.=20
<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The long=20
type represents signed 64-bit integers with values between =
-9223372036854775808=20
and 9223372036854775807. <o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The ulong=20
type represents unsigned 64-bit integers with values between 0 and=20
18446744073709551615. <o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1">&nbsp;&nbsp; =
</SPAN>The char=20
type represents unsigned 16-bit integers with values between 0 to 65535. =

<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">The set of possible =
values for=20
the char type corresponds to the Unicode character set. All this is =
copied=20
straight from the documentation. Fastest paragraph ever written. It took =
exactly=20
1.2 seconds to accomplish the copy and paste.<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">As a re-revision, for =
unary=20
operators +,- and ~ and the binary operators the operand/s are first =
converted=20
to an int, uint, long or ulong. These datatypes can fully represent the =
range of=20
values. The unary operator, however, first converts the operand only to =
an int=20
or long. The relational operators only deals with a bool as the result=20
value.<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>char j =3D 'A';<o:p></o:p></P>
<P class=3DCprg>char i =3D (char)65;<o:p></o:p></P>
<P class=3DCprg>char k =3D 65;<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(7,10): error CS0031: Constant value '65' cannot =
be=20
converted to a 'char'<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =

⌨️ 快捷键说明

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