📄 chapter 4.mht
字号:
into an=20
object without the compiler screaming errors at us. We cannot do the =
reverse,=20
that is convert an object b, into a long even though b also stands for a =
long.=20
Tough luck. <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">There is an implicit =
conversion=20
available from the derived class to the base class. We do not have to =
create one=20
as it is already present. A lot more on implicit conversion is explained =
in one=20
of the coming chapters.<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"><![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 : yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static implicit operator yyy ( xxx =
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>}<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(6,15): error CS0553: 'xxx.implicit operator =
yyy(xxx)':=20
user-defined conversion to/from base class<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">As such, a conversion =
is already=20
available so we cannot create one ourselves. <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>public static implicit operator xxx ( yyy =
a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>return new 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 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]> <o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(3,15): error CS0554: 'yyy.implicit operator =
xxx(yyy)':=20
user-defined conversion to/from derived class<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">It shows the same =
error and it is=20
proof that we ignored the earlier error message. We cannot convert from =
or to a=20
base/derived class ever as it is handled internally for us.=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>{<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 =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>xxx b =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>a =3D b;<o:p></o:p></P>
<P class=3DCprg>b =3D (xxx)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>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 : 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=3DCbase><SPAN style=3D"COLOR: windowtext">There exists an =
explicit cast to=20
convert a base class to a derived class and an implicit cast to convert =
a=20
derived class to a base class.<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">Definite=20
Assignment<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=3DCbase><SPAN style=3D"COLOR: windowtext">A variable is =
definitely assigned=20
if the compiler can prove that either the variable has been =
automatically=20
assigned or it has been the target of at least one assignment. The rules =
for=20
definite assignment are as 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=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1"> =
</SPAN>Every=20
variable that has been initially assigned is also definitely assigned.=20
<o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1"> =
</SPAN>Before we=20
reach a variable by any possible path, a simple assignment statement is=20
encountered where this variable is the left operand. <o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1"> =
</SPAN>We call a=20
function where the variable is being passed as a left =
operand<o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DCbullets>=95<SPAN style=3D"mso-tab-count: 1"> =
</SPAN>Finally, a=20
local variable includes a variable initializer.<SPAN=20
style=3D"mso-spacerun: yes"> </SPAN><o:p></o:p></P>
<P class=3DCbullets><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">For a structure, the =
state of a=20
variable is tracked individually as well as collectively. Thus, for a =
structure=20
to be definitely assigned, we need all the individual members to be =
definitely=20
assigned.<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">There are a large =
number of=20
reasons for the variable to be definitely assigned. =
<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 a variable =
is accessed=20
for its value, it must have been definitely assigned otherwise an =
undefined=20
variable error will take place. An unassigned variable cannot be =
accessed for=20
its value. A variable can be initialized in three ways, by being on the =
left of=20
an assignment, being passed as an out parameter or finally appearing as =
a left=20
operand in a structure access. <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 told you earlier =
that a ref=20
parameter can be used only in a function invocation if it has been =
initialized=20
earlier outside the function. Thus, ref parameters are always definitely =
assigned. Out parameters for a function must be initialized before the =
function=20
terminates. Thus, an out parameter will always be considered definitely =
assigned=20
when we exit the function.<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>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int i,j;<o:p></o:p></P>
<P class=3DCprg>try<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>i =3D 10;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>catch<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(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=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(15,26): error CS0165: Use of unassigned local =
variable=20
'i'<o:p></o:p></P>
<P class=3DCoutput>a.cs(15,36): error CS0165: Use of unassigned local =
variable=20
'j'<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 spite of =
explicitly=20
initializing the variable i in the try block, for the catch the variable =
i is=20
not initialized. The compiler for some reason believes that we can enter =
the=20
catch block without executing the assignment statement of the try block. =
Thus,=20
in the catch where we are using the variables i and j, the compiler =
believes=20
that they have not been initialized at all and thus generates the 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=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>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int i,j;<o:p></o:p></P>
<P class=3DCprg>try<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>i =3D 10;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>catch<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>finally<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(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=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(18,26): error CS0165: Use of unassigned local =
variable=20
'i'<o:p></o:p></P>
<P class=3DCoutput>a.cs(18,36): error CS0165: Use of unassigned local =
variable=20
'j'<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">Ditto for the finally =
which=20
always is called at the end of the try. Hence, before you enter a=20
try-catch-finally block, please initialize all the variables. The above=20
conclusions, we repeat have been reached by a process known as static =
flow=20
analysis. <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><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P class=3DCprg><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></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>public void abc()<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int x=3D1,y=3D2;int i;<o:p></o:p></P>
<P class=3DCprg>if ( x >=3D 2 <SPAN=20
style=3D"FONT-FAMILY: 'Bookman Old Style'">&&</SPAN> (i=3Dy) =
!=3D=20
1)<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("one " + i);<o:p></o:p></P>
<P class=3DCprg>else<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("two " + i);<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("three " + i);<o:p></o:p></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -