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

📄 chapter 4.mht

📁 C# Nuts and Bolt是学习C#的极好教程
💻 MHT
📖 第 1 页 / 共 5 页
字号:
!supportEmptyParas]><![endif]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">A char constant has =
to be written=20
as a character literal or a number with a cast. If you do not, the above =
error=20
will be your reward.<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"><![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 l =3D (char)65;<o:p></o:p></P>
<P class=3DCprg>char m =3D '\x0041';<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(j + " " + l + " " + =
m);<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>Output</U><o:p></o:p></P>
<P class=3DCoutput>A<SPAN style=3D"mso-spacerun: yes">&nbsp; =
</SPAN>A<SPAN=20
style=3D"mso-spacerun: yes">&nbsp; </SPAN>A<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">As there are many =
ways to skin a=20
cat, there are many ways to do the same thing in C#. Thus, we can also =
use the=20
hexadecimal notation to initialize a char, provided we are using =
literals. Also,=20
a cast is the perfect panacea for all our problems. Any errors resulting =
due to=20
conversions can always be removed using a cast.<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">Floating=20
Point<o:p></o:p></SPAN></B></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 two different =
floating=20
point types in the C# programming language, Float and its elder, smarter =
brother=20
double. The computer world got together and came out with a worldwide =
standard=20
on storing numbers with decimal places in memory. They have to be stored =

differently from a number. This standard could not go nameless and hence =
it was=20
called the IEEE 754 standard. It is freely available to all and stores =
floats=20
and doubles as 32 bit single precision and 64 bit double precision =
numbers in=20
memory. <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">Zero denotes an =
absence of=20
anything. If something is not present, then how in the first place, can =
we=20
detect it? Do we have a concept of positive zero and negative zero? =
These are=20
questions to be answered by philosophers and the mathematics doctors.=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">Most of the time =
zeroes are the=20
same but at times, albeit rarely, they are different. If we have the =
time, we=20
will give you a short exposition on zeroes. To complicate matters even =
further,=20
infinity also cannot be measured. The moment you can, it stops being =
infinite.=20
It is like the sound of a one hand clap. Thus we have like zeroes, =
positive and=20
negative infinity. 1.0/0.0 is +ve infinity and -1.0/0.0 is negative =
infinity.=20
When we divide a zero by a zero. What should we get as the answer. =
Obviously=20
something that is Not a Number or NAN, which is also an invalid number.=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">Let us now understand =
the=20
vastness of numbers that a floating point type can store. If the formula =
can be=20
written as s * m * 2^e where s is either 1 or -1. The range of m is from =
o to 2=20
^24 for a float and e lies form -149 to 104. For a double m lies form 0 =
to 2^53=20
and e form -1075 to 970. To put the above numbers in the right =
perspective, a=20
float can represent values ranging from 1.5 + 10"45 to 3.4 + 1038 with a =

precision of 7 digits whereas the double data type is nearly double that =
of the=20
float. It is from 5.0 + 10"324 to 1.7 + 10308 with a precision of 15-16=20
digits.<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 you walked in late =
to the=20
party, for binary operators, the rule is very simple. First, there is a=20
conversion to double and then to a float for any one of the operands =
that is a=20
integer type and the other, a double or float. Floating point types hate =

producing exceptions and thus do not throw them at all. They show their=20
displeasure in other ways. If the answer is too small for the =
destination type,=20
a +ve or a -ve zero is the final result and if it is too large, it will =
be +ve=20
or -ve infinity. If the answer does not follow the IEE 754 =
specifications, a NAN=20
is outputted. A NaN does not like to coexist with any other number, and =
thus if=20
any operand produces a NaN, the result of the operation is a=20
NaN.<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">C# is very flexible =
in the=20
handling of floating point types in memory. The above range is only =
suggestive=20
at the lower end. The compiler only enforces a bare minimum and not a =
maximum.=20
Hardware architectures keep evolving and there are some, where a =
floating point=20
number is stored internally using more memory than what is specified by =
the IEEE=20
754 specifications. If the compiler had to adhere to the IEEE format, it =
would=20
slow down the machine even though we are asking the computer to use less =
memory=20
to do the calculations. Thus, the compiler allows us even more leeway =
here. It=20
lets us more than double the range of the floating point and also does =
not=20
compromise on speed. More is good, less is bad!<o:p></o:p></SPAN></P>
<P class=3DCbase><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: =
Tahoma">Decimal=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">If you have been =
impressed by the=20
floating point types, now is the time to remove the scales from your =
eyes. For a=20
large number of financial and monetary computations, the floating point =
types=20
are not accurate enough and introduce a rounding off error. The decimal =
type=20
needs 128 bits or 16 bytes of memory to store itself. The range, it can =
handle=20
all by itself, starts from 1.0 + 10"28 to approximately 7.9 + 1028 with =
28-29=20
significant digits. Using the same formula earlier s * m * 10^e, s is 1 =
or -1 ,=20
m from 0 to 2 ^ 96 and e from -28 to 0. The decimal type unlike floating =
point=20
does not understand signed zeros, infinities and =
NaN's.<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">A decimal is =
represented by a 96=20
bit integer scaled by a power of 10. If the absolute value is less than =
1.0m,=20
the number is accurate to the 28th decimal place. Great. But if the =
value is=20
greater than 1, the value is exact as per the 28 or 29 digit. The =
problem with a=20
float/double is its ability to represent numbers like 10/3 or .1 in an =
accurate=20
fashion. These are infinite fractions and are prone to round off errors. =
The=20
decimal format does not have these problems.<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">For a binary operator =
if any of=20
the operands is a decimal, the other must either be a decimal or a =
integer and=20
not a floating point number in any circumstance. A useful tidbit. All =
decimal=20
numbers are rounded off to their nearest possible value and if two =
values are=20
equally close, the one that has an even number in the least significant =
or last=20
digit is used.<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 a decimal =
operation produces=20
an answer that is very small for the decimal format after rounding, =
unlike a=20
floating point type, the result is zero. However, if the result is too =
large, an=20
Overflow Exception is thrown. Unlike floats, the decimal has no qualms =
about=20
using exceptions. <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">Now, to sum up the =
differences=20
between floats and decimals. The decimal type has greater precision or =
accuracy=20
than the floating point type but at the cost of a smaller range. Thus if =
we=20
convert from a floating point to a decimal, we may get an overflow as =
the float=20
can store higher values. Conversion from decimal to float can cause loss =
of=20
precision instead. Thus, there exists no known conversion between =
floating point=20
types and decimals. You can only mix them in an expressions by using an =
explicit=20
cast.<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">A bool is antisocial, =
hating=20
interaction with any other type. A bool cannot be converted to any other =
type,=20
not even an integer. It prefers its solitude.<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>bool i =3D false;<o:p></o:p></P>
<P class=3DCprg>int j =3D (int)i;<o:p></o:p></P>
<P class=3DCprg>i =3D (bool)1;<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(6,10): error CS0030: Cannot convert type 'bool' =
to=20
'int'<o:p></o:p></P>
<P class=3DCoutput>a.cs(7,6): error CS0030: Cannot convert type 'int' to =

'bool'<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">When in doubt, use a =
cast. It=20
does not work with bool. No explicit conversions exist from a bool to =
any other=20
type. Read our lips. That's it.<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">Boxing =
and=20
Unboxing<o:p></o:p></SPAN></B></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">This is a central =
concept that=20
unifies the type system of C#, which consists of value and reference =
types.=20
Boxing is a way by means of which a value type becomes a reference type =
and=20
unboxing does the reverse, i.e. it converts a reference type into a =
value type.=20
It binds these two types. The resulting reference type is an object =
naturally.=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">Boxing, thus, =

⌨️ 快捷键说明

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