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

📄 chapter 7.mht

📁 C# Nuts and Bolt是学习C#的极好教程
💻 MHT
📖 第 1 页 / 共 5 页
字号:
!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>object i;<o:p></o:p></P>
<P class=3DCprg>xxx x =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>yyy y =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>i =3D<SPAN style=3D"mso-spacerun: yes">&nbsp; </SPAN>7 =
&gt; 3<SPAN=20
style=3D"mso-spacerun: yes">&nbsp; </SPAN>? x : y ;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i);<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 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]>&nbsp;<o:p></o:p></SPAN></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(8,15): error CS0173: Type of conditional =
expression can't=20
be determined because there is no implicit conversion between 'xxx' and=20
'yyy'<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 compiler tries to =
convert an=20
xxx type object to a yyy type object but there is no such implicit =
conversion=20
available. <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>object i;<o:p></o:p></P>
<P class=3DCprg>xxx x =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>yyy y =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>i =3D<SPAN style=3D"mso-spacerun: yes">&nbsp; </SPAN>7 =
&gt; 3<SPAN=20
style=3D"mso-spacerun: yes">&nbsp; </SPAN>? x : y ;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i);<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>public static implicit operator yyy ( xxx =
a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("operator");<o:p></o:p></P>
<P class=3DCprg>return new 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 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]>&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=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>operator<o:p></o:p></P>
<P class=3DCoutput>yyy<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 do not receive any =
error as=20
the compiler converts the xxx object to a yyy object using the implicit =
operator=20
supplied. The implicit operator performs its job by converting the xxx =
to=20
another data type, in this case yyy. Finally, the ToString function of =
the=20
object i is called which displays its data type as =
yyy.<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>object i;<o:p></o:p></P>
<P class=3DCprg>xxx x =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>yyy y =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>i =3D<SPAN style=3D"mso-spacerun: yes">&nbsp; </SPAN>7 =
&lt; 3<SPAN=20
style=3D"mso-spacerun: yes">&nbsp;&nbsp; </SPAN>? x : y ;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i);<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 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>System.Console.WriteLine("operator");<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=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>operator<o:p></o:p></P>
<P class=3DCoutput>xxx<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 have brought about =
two=20
changes. First, we've changed the condition to return false so that the =
result=20
now is a yyy object. Next is that the implicit operator is used to =
convert the=20
data type to xxx. So much done, the compiler now wants to determine the=20
controlling type of the conditional operator. It tries to convert one to =
another=20
and if it fails, it flags an error. It tries the type conversion from =
the 2nd=20
operand to 3rd and then vice versa.<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>i =3D<SPAN style=3D"mso-spacerun: yes">&nbsp; </SPAN>7 =
&gt;<SPAN=20
style=3D"mso-spacerun: yes">&nbsp; </SPAN>3<SPAN=20
style=3D"mso-spacerun: yes">&nbsp;&nbsp; </SPAN>? x : y ;<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">If we reverse the =
logical=20
condition, the resultant output now shows: <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=3DCoutput><U>Output</U><o:p></o:p></P>
<P class=3DCoutput>xxx<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 compiler has a =
mind of its=20
own. It realizes that the logical condition is true and the result is x, =
an xxx=20
object. As it is, i is already an xxx object and the conversion =
available is=20
more to convert from yyy to xxx, so it doesn't use it. Hence, the =
operator is=20
not called. When the condition turns out to be false, as seen earlier, =
the=20
result is a yyy object. This needs conversion so we bring in a user =
defined=20
operator to do 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=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>object i;<o:p></o:p></P>
<P class=3DCprg>xxx x =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>yyy y =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>i =3D<SPAN style=3D"mso-spacerun: yes">&nbsp; </SPAN>7 =
&lt; 3<SPAN=20
style=3D"mso-spacerun: yes">&nbsp;&nbsp; </SPAN>? x : y ;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i);<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>public static implicit operator yyy ( xxx =
a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("operator yyy");<o:p></o:p></P>
<P class=3DCprg>return new 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 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>System.Console.WriteLine("operator xxx");<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=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(8,16): error CS0172: Type of conditional =
expression can't=20
be determined because 'xxx' and 'yyy' both implicitly convert to each =
other<SPAN=20
style=3D"mso-spacerun: yes">&nbsp;&nbsp; </SPAN><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 problems of =
plenty! We have=20
supplied an operator to convert a yyy object to an xxx object and vice =
versa.=20
This confuses the life out of the compiler and therefore we get an error =
as both=20
operators can do the same job. If the compiler chooses any one of them, =
it could=20
be accused of being impartial. Nor does it like gambling. So it flags an =

error.<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>xxx i;<o:p></o:p></P>
<P class=3DCprg>xxx x =3D new xxx();<o:p></o:p></P>
<P class=3DCprg>yyy y =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>int a =3D 4, b=3D 6;<o:p></o:p></P>
<P class=3DCprg>i =3D<SPAN style=3D"mso-spacerun: yes">&nbsp; </SPAN>a =
&gt; b ? x : y=20
;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(i);<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>public static implicit operator yyy ( xxx =
a)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("operator");<o:p></o:p></P>
<P class=3DCprg>return new 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 yyy { <o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(9,6): error CS0029: Cannot implicitly convert =
type 'yyy'=20
to 'xxx'<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 above program =
tours you into=20
the mind of a compiler. It gives you a good insight into its workings. =
The=20
conditional operator has a condition whose result can be estimated only =
at run=20
time. Thus the compiler cannot fathom whether the type of the operator =
will be=20
xxx or yyy. The compiler assumes the result to be a yyy object. Under =
this=20
pretext, the following result of the conditional too has to be stored in =
a xxx=20
type object. However, there is no such conversion available hence it =
errs. The=20
compiler could have assumed the conditional to be true thereby resulting =
in an=20
xxx object. Then no conversions would be required. Well, it performs =
both these=20
checks, hence it fails just to be on the side of your=20
safety.<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 we change the data =
type of i=20
from an xxx to yyy, we get no errors as the compiler assumes that the =
result of=20
the operator will be a xxx as the worst case possible. It has an =
operator to=20
convert to a yyy. Move the operator function to the yyy class and the =
error will=20
now read as given below.<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=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(9,6): error CS0029: Cannot implicitly convert =
type 'xxx'=20
to 'yyy'<o:p></o:p></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><![if =
!supportEmptyParas]><![endif]>&nbsp;<o:p>

⌨️ 快捷键说明

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