📄 chapter 7.mht
字号:
converted to a 'int' (use 'unchecked' syntax to override)<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 int k =3D =
unchecked((int)2147483648);<o:p></o:p></P>
<P class=3DCprg>public int l =3D (int)2147483648;<o:p></o:p></P>
<P class=3DCprg>public int m =3D =
checked((int)2147483648);<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>
<P class=3DCoutput>a.cs(4,17): error CS0221: Constant value '2147483648' =
cannot be=20
converted to a 'int' (use 'unchecked' syntax to override)<o:p></o:p></P>
<P class=3DCoutput>a.cs(5,25): error CS0221: Constant value '2147483648' =
cannot be=20
converted to a 'int' (use 'unchecked' syntax to override)<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 only difference =
between the=20
two programs is that in the first program, the variables are const =
whereas in=20
the second program they are not. The error messages are the same. We =
could not=20
combine them into one program as at times the compiler stops at the =
first error=20
message it encounters. Sometimes it does not display all of them.=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">We are trying to cast =
a number=20
larger than an int to an int. Thus we see the error. Reduce the number =
by one=20
and the error like a nightmare disappears. This happens only for checked =
and not=20
unchecked. <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">Remember for compile =
time checks=20
the default is checked in case you have become absentminded along the =
way. The=20
++, --<SPAN style=3D"mso-spacerun: yes"> </SPAN>operators act on =
overflow in=20
the same way.<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()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int k =3D 65537;<o:p></o:p></P>
<P class=3DCprg>short j =3D (short)k;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(j);<o:p></o:p></P>
<P class=3DCprg>short l =3D checked((short)k);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(l);<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>1<o:p></o:p></P>
<P class=3DCoutput>Exception occurred: System.OverflowException: An =
exception of=20
type System.OverflowException was thrown.<o:p></o:p></P>
<P class=3DCoutput><SPAN style=3D"mso-spacerun: yes"> =
</SPAN>at=20
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">The same rule also =
applies to=20
conversions. We cannot directly convert an int to a short as the short =
is=20
smaller in range of values than an int. Hence the cast. The compiler is=20
oblivious of the error we have made and when we run the program, the =
short j=20
gets truncated. This is so because the number gets divided by 65536, but =
as the=20
default is unchecked no exception is thrown. In the second case, =
however, we=20
have explicitly used the operator checked.<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">Remember, the rules =
for a=20
constant and non-constant expression are different. Run time behaviour =
can be=20
changed by compile time switches. What we've learnt is the default =
behaviour as=20
of this version. The behaviour can either be checked or unchecked but =
there is=20
no in between option. The checked and unchecked operators are not nosy =
parkers.=20
They restrict themselves only to the () brackets. Their domain does not =
extend=20
beyond that.<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><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: Tahoma">Is=20
operator<o:p></o:p></SPAN></B></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 yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<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>yyy a<SPAN style=3D"mso-spacerun: yes"> </SPAN>=3D =
new=20
yyy();<o:p></o:p></P>
<P class=3DCprg>if ( a is yyy)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy");<o:p></o:p></P>
<P class=3DCprg>if ( a is System.String)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("string");<o:p></o:p></P>
<P class=3DCprg>if ( a is object)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("object");<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 Warning</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(9,6): warning CS0183: The given expression is =
always of=20
the provided ('yyy') type<o:p></o:p></P>
<P class=3DCoutput>a.cs(11,6): warning CS0184: The given expression is =
never of=20
the provided ('string') type<o:p></o:p></P>
<P class=3DCoutput>a.cs(13,6): warning CS0183: The given expression is =
always of=20
the provided ('object') type<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>yyy<o:p></o:p></P>
<P class=3DCoutput>object<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 'is' operator is =
used to=20
determine the run time type of an object or its compatibility with =
another type.=20
It returns either a true or false, that is, a boolean. As the object a =
is an=20
instance of class yyy, 'a is yyy' is obviously true. Also, a not being a =
string=20
will not execute the second System.Writeln. Finally, as all classes =
derive from=20
object the last if results in a true hence object is displayed.=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 warnings are =
issued as even a=20
blind man will tell you that the conditions in the if statement are =
determinable=20
at run time. Anything that the compiler can't figure out and can be =
determined=20
at run time is flagged as a warning. <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 yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<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>if ( yyy is a)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("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]> <o:p></o:p></SPAN></P>
<P class=3DCprg><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCprg>a.cs(8,6): error CS0118: 'yyy' denotes a 'class' where a =
'variable' was expected<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 operands to the =
'is' operator=20
cannot be reversed. It first requires the name of the object and then =
the class=20
or type name. <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 yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class xxx<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<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>zzz a =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>yyy b =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>xxx c =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>a.abc(b);<o:p></o:p></P>
<P class=3DCprg>a.abc(c);<o:p></o:p></P>
<P class=3DCprg>a.abc(20);<o:p></o:p></P>
<P class=3DCprg>a.abc("hi");<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public void abc(object o)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>if ( o is yyy)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy " + o);<o:p></o:p></P>
<P class=3DCprg>if ( o is xxx )<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy " + o);<o:p></o:p></P>
<P class=3DCprg>if ( o is string )<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy " + o);<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>yyy yyy<o:p></o:p></P>
<P class=3DCoutput>yyy xxx<o:p></o:p></P>
<P class=3DCoutput>yyy hi<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 compiler gives us =
no=20
warnings. Whenever we call a function, we need to be very particular =
about the=20
data type of the parameter or else it will generate an error. The first=20
exception to this rule can be when objects are cast to each other. The =
second=20
exception can be with objects that are derived from each other. As all =
classes=20
derive from the Object class, the parameter set in function abc can be =
of object=20
type, which means that abc can now receive different parameter types. It =
will=20
not throw any error. <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 have three is =
operators=20
checking whether o is a yyy, xxx or string. The WriteLine function also =
displays=20
the object. Remember we are checking o with different class names and =
the if=20
statement will be true only once in each case.<SPAN=20
style=3D"mso-spacerun: yes"> =
</SPAN><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 yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>class xxx<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<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>zzz a =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>xxx c =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>if (c is yyy)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("yyy " + c);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCoutput><U>Compiler Warning</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(13,5): warning CS0184: The given expression is =
never of=20
the provided ('yyy') type<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">In the above case, =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -