📄 chapter 2.mht
字号:
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static implicit operator int ( 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 1;<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>operator<o:p></o:p></P>
<P class=3DCoutput>first<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 way to =
eliminate the=20
error is by overloading an int operator with class yyy. This will =
convert the=20
yyy object into an int. As the function returns the resultant int as 1, =
the=20
second case will be called. In a real life scenario, the return value =
depends=20
upon some instance variable. Thus each time the switch statement will =
call the=20
int operator to convert the yyy object into an int and then call the =
case=20
statements depending on the resultant value.<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>public 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 z =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>yyy a =3D new yyy();<o:p></o:p></P>
<P class=3DCprg>z.abc(a);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(yyy i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("second");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("first");<o:p></o:p></P>
<P class=3DCprg>break;<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=3DCprg>class yyy<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>public static implicit operator int ( 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 1;<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>public static implicit operator byte ( 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 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>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(11,9): error CS0151: A value of an integral type =
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">We really got off the =
wrong side=20
of our bed today. We therefore added two operators, one that converts a =
yyy to=20
an int and the other that converts a yyy to a byte. As the case has no=20
preferences of a byte over a int, it gets confused and does not know =
which=20
operator to call. Since both are equally probable, it flags an error. =
The switch=20
expression decides on the governing type of the statement.=20
<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext">There are many more =
rules to=20
switch that sensibly state that there can be no two defaults in a =
switch. The=20
end-point, that is, the end of the switch must never be reachable or =
else it=20
will result in a compile time 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>public 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 z =3D new zzz();<o:p></o:p></P>
<P class=3DCprg>z.abc(2);<o:p></o:p></P>
<P class=3DCprg>z.abc(3);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("first");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>case 3:<o:p></o:p></P>
<P class=3DCprg>case 2:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("second");<o:p></o:p></P>
<P class=3DCprg>break;<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>Output</U><o:p></o:p></P>
<P class=3DCoutput>second<o:p></o:p></P>
<P class=3DCoutput>second<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">You can have many =
case statements=20
bunched up together. There will be no errors if you do so. In the above =
switch,=20
for the values 2 and 3, the same code is executed. Thus multiple labels =
are=20
permitted in the switch. case 2: can be replaced by a default: and it =
would=20
still result in the same answer. Though it is not a good programming =
style as=20
removing the case 2: would not change the answer and default means none =
of the=20
above. The order of the case statements is irrelevant to the final =
answer so=20
changing the order doesn't change anything. For that matter, the default =
came=20
first for all that C# cares.<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 governing type of =
a switch=20
can also be a string. The case statement values are however case =
sensitive. We=20
can also use null as we are talking real objects here. =
<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">Within a case =
statement we can=20
use declaration statements i.e. create variables etc. =
<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>public 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>a.abc(0);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>int j=3D 7;<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(j);<o:p></o:p></P>
<P class=3DCprg>break;<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>Output</U><o:p></o:p></P>
<P class=3DCoutput>7<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>public 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>a.abc(0);<o:p></o:p></P>
<P class=3DCprg>}<o:p></o:p></P>
<P class=3DCprg>void abc(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>int j=3D 7;<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>int k=3D 7;<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine(k);<o:p></o:p></P>
<P class=3DCprg>break;<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=20
class=3DCoutput><U><![if =
!supportEmptyParas]><![endif]> <o:p></o:p></U></P>
<P class=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(12,1): error CS1523: The keyword case or default =
must=20
precede code in switch block<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">For some reason, we =
are not=20
allowed to create variables in the switch statement. Had we been =
allowed, then=20
the variable would have scope in the entire switch. As we are allowed to =
create=20
them only in a case, their scope is restricted to that case only. In the =
above=20
program, remove the line int j =3D 7 and relish the error as seen=20
below.<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(16,26): error CS0165: Use of unassigned local =
variable=20
'k'<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><B><SPAN=20
style=3D"FONT-SIZE: 14pt; COLOR: windowtext; FONT-FAMILY: Tahoma">Empty=20
<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 lot of life goes by =
without=20
doing anything. As shown in one of the earlier examples, the while loop =
did=20
absolutely nothing. Whenever a statement does nothing we call it an =
empty=20
statement. Perfectly valid in C#. Execution of an empty statement simply =
transfers control to the statement's end point.<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>public 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(int i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>goto aa;<o:p></o:p></P>
<P class=3DCprg>aa: <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,1): error CS1525: Invalid expression term=20
'}'<o:p></o:p></P>
<P class=3DCoutput>a.cs(11,1): error CS1002: ; 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">We normally create =
labels at the=20
end of a function so that all code can jump to the end if they so choose =
to.=20
Here we have created a label aa on the last line of function abc. We get =
an=20
error as C# demands at least one statement following the label.=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">C# demands a =
statement so we have=20
no choice but to give it an empty statement. The best way to cut short =
an=20
argument is by doing what C# wants and give it a statement, albeit, an =
empty=20
one, one that does nothing.<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>public class zzz<SPAN style=3D"mso-spacerun: yes"> =
</SPAN>{<o:p></o:p></P>
<P class=3DCprg>public static void Main()<o:p></o:p></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -