📄 chapter 9.mht
字号:
although both the operators were applicable, it does not give us an =
error. In=20
the above types, a short is not mentioned. Recall that sometime back, we =
spoke=20
of a short being converted into a int. Operators are allowed to throw an =
exception. If one takes place then the processing stops. =
<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>byte [] b =3D null;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(b[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]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>Unhandled Exception: System.NullReferenceException: =
Value null=20
was found where an instance of an object was required.<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">Like a delegate, if =
we try and=20
access any null object, an exception is thrown. No compile time checks =
are=20
performed for null as the value. Maybe the next version of the compiler =
will=20
check for a null and it is on our wish list for Santa Claus. Ditto if we =
try and=20
exceed the bounds of an array. The exception thrown is<SPAN=20
style=3D"mso-spacerun: yes"> </SPAN>IndexOutOfRangeException =
instead.=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=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 pqr(ref object x)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("pqr " + x);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public static void abc(object x)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("abc " + x);<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>object[] a =3D new object[2];<o:p></o:p></P>
<P class=3DCprg>object[] b =3D new string[2];<o:p></o:p></P>
<P class=3DCprg>abc(a[0]);<o:p></o:p></P>
<P class=3DCprg>abc(b[1]);<o:p></o:p></P>
<P class=3DCprg>pqr(ref a[0]);<o:p></o:p></P>
<P class=3DCprg>pqr(ref b[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]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>abc <o:p></o:p></P>
<P class=3DCoutput>abc <o:p></o:p></P>
<P class=3DCoutput>pqr <o:p></o:p></P>
<P class=3DCoutput><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DCoutput>Unhandled Exception: =
System.ArrayTypeMismatchException:=20
Exception of type System.ArrayTypeMismatchException 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 rules of array =
co-variance=20
allow an array to be populated by any data type provided there exist an =
implicit=20
conversion between them. Array b is an array of objects and there exists =
an=20
implicit conversion from a string to an object. Hence, we can initialize =
an=20
array of objects to an array of strings. The array object b is not an =
array of=20
objects but one that comprises of an array of strings. The compile time =
data=20
type may be that of an object but the run time data type must be that of =
a=20
string. <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">Whenever we have a =
ref parameter=20
being passed a reference, we use an array instead. The compiler is smart =
enough=20
to perform a run time check on the data type being passed as an array =
now can=20
hold dual data types. In the last call to function pqr, we are passing =
b[0]=20
which at compile time is an object but at run time is a string. Thus, an =
exception is thrown. Remember exceptions can only be thrown at run time. =
This=20
holds true only for ref parameters and not value parameters.<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 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>int x;<o:p></o:p></P>
<P class=3DCprg>void abc( int a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>x =3D 1;<o:p></o:p></P>
<P class=3DCprg>if (a > 10)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>float x =3D 1.0;<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=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(10,7): error CS0136: A local variable named 'x' =
cannot be=20
declared in this scope because it would give a different meaning to 'x', =
which=20
is already used in a 'parent or current' scope to denote something=20
else<o:p></o:p></P>
<P class=3DCoutput>a.cs(10,11): error CS0029: Cannot implicitly convert =
type=20
'double' to 'int'<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">A block is =
represented by a {}=20
braces. Any variable or identifier created within a block must primarily =
be=20
unique within that block. Then it must also be unique within the =
immediate=20
enclosing block. Thus in both blocks, the name must be unique and =
therefore=20
refer to the same entity. The meaning of the identifier must remain the =
same=20
within the block. <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 get an error in =
the above=20
program as in the outer block we have an int x and in the inner block, =
within=20
the if statement we have another variable called x again. The second =
error comes=20
in handy as it very clearly states that it assumes the variable x to be =
an int=20
within the if statement. The golden rule again. We cannot have a =
variable with=20
the same name in the inner and outer block. If we remove the statement x =
=3D 1=20
from the above program we will get a slightly different error 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=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(10,11): error CS0664: Literal of type double =
cannot be=20
implicitly converted to type 'float'; use an 'F' suffix to create a =
literal of=20
this 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=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>int x;<o:p></o:p></P>
<P class=3DCprg>void abc( int a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>if (a > 10)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>x =3D 1;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>else<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>double x =3D 1.0;<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=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(15,8): warning CS0219: The variable 'x' is =
assigned but=20
its value is never used<o:p></o:p></P>
<P class=3DCoutput>a.cs(6,5): warning CS0169: The private field 'zzz.x' =
is never=20
used<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 errors=20
as the variable x created outside of a function in the outer block is =
not used=20
in the main function block. It is only used in the if statement. Since =
the else=20
block of the if statement, which is at the same level is not executed, =
the=20
compiler adjourns after giving a few warnings. <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>}<o:p></o:p></P>
<P class=3DCprg>int x;<o:p></o:p></P>
<P class=3DCprg>void abc( int a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>x =3D 3;<o:p></o:p></P>
<P class=3DCprg>if (a > 10)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>x =3D 1; <o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>else<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>double x =3D 1.0;<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=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(16,8): error CS0136: A local variable named 'x' =
cannot be=20
declared in this scope because it would give a different meaning to 'x', =
which=20
is already used in a 'parent or current' scope to denote something=20
else<o:p></o:p></P>
<P class=3DCoutput>a.cs(16,12): error CS0029: Cannot implicitly convert =
type=20
'double' to 'int'<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">If we knowingly =
initialize the=20
instance variable x in the function abc, we are using the one created in =
the=20
outer block. So, we are not allowed to recreate the same variable x the =
inner=20
block under any circumstance. If you comment the line with x=3D1 or =
double x=3D1.0,=20
the compiler will give you warnings. This proves that the compiler is =
confused=20
on the variable it should work with while initializing x to 1 as there =
are two=20
variables available by the same 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 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>void abc( bool a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>if (a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int i =3D 0;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>int i =3D 3;<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:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -