📄 chapter 2.mht
字号:
<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>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>while ( true) ;<o:p></o:p></P>
<P class=3DCprg>case 1:<o:p></o:p></P>
<P class=3DCprg>return ;<o:p></o:p></P>
<P class=3DCprg>case 3:<o:p></o:p></P>
<P class=3DCprg>throw new System.Exception();<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=3DCbase><SPAN style=3D"COLOR: windowtext">The documentation =
very clearly=20
states that the end point of the case statement must not be reached. =
Thus, any=20
lines of code that prevent the last line of the case being executed is =
legal=20
tender. A while (true) goes on forever, This make sure that you do not =
leave the=20
while forever and thus the endpoint of the case never gets reached. =
Though it=20
does not make any sense at all as i being zero will enter the while and =
never=20
leave the case. <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">A return is more =
logical at this=20
point as it can exit the function altogether and not just the case. It =
may be=20
what you had in mind, however. A throw statement also breaks the case, =
the try=20
catch is not mandatory here. Just because the compiler does not complain =
with a=20
warning or an error it does not mean that you are home scott=20
free.<o:p></o:p></SPAN></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>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>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("zero");<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>case 0:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("one");<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>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(13,6): error CS0152: The label 'case 0:' already =
occurs in=20
this switch statement<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">Somebody somewhere =
did some=20
research and found out that people fell asleep while writing code. When =
they=20
woke up, they rewrote the same code again. Thus, we had two copies of =
the same=20
case in the program. C# peeks into human mind and is aware that you'd =
fall=20
asleep at the wheel. <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">Thus, it checks =
whether you are=20
writing the same code all over again. If it catches you in the act, you =
will see=20
an error like the one above. It makes no logical sense having two case=20
statements with the same value. If the value of i happens to be zero =
then there=20
is confusion as in which case should C# execute. The first, the second, =
or both,=20
or, even better, none. Decisions, decisions, they are so difficult to =
make, so=20
the C# compiler drops by an 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>int j =3D 10;<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>switch (i)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case j:<o:p></o:p></P>
<P class=3DCprg>break;<o:p></o:p></P>
<P class=3DCprg>case j > 4:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("zero");<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>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(11,6): error CS0150: A constant value is=20
expected<o:p></o:p></P>
<P class=3DCoutput>a.cs(13,6): error CS0029: Cannot implicitly convert =
type 'bool'=20
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">Here we want to =
convince you that=20
an if statement in our humble/esteem opinion is better than a switch =
statement.=20
In a case statement, we have to check the variable with a constant =
value. We are=20
not permitted to use a variable or a logical condition. They have to be =
constant=20
predefined values only. This limits our flexibility in writing complex=20
conditions thereby preventing us from writing more abstract more =
intelligent=20
code.<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(7);<o:p></o:p></P>
<P class=3DCprg>z.abc(70);<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 > 8)<o:p></o:p></P>
<P class=3DCprg>{<o:p></o:p></P>
<P class=3DCprg>case 4 > 5:<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 4 < 5:<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>first<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">We could not for the =
life of us=20
figure out, why the above program works. So we added the following lines =
at the=20
end of the second case statement ergo producing an =
error.<o:p></o:p></SPAN></P>
<P class=3DCbase><SPAN style=3D"COLOR: windowtext"><SPAN=20
style=3D"mso-spacerun: yes"> </SPAN><o:p></o:p></SPAN></P>
<P class=3DCprg>case 40 > 5:<o:p></o:p></P>
<P class=3DCprg>System.Console.WriteLine("third");<o:p></o:p></P>
<P class=3DCprg>break;<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">and we got the =
following=20
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=3DCoutput><U>Compiler Error</U><o:p></o:p></P>
<P class=3DCoutput>a.cs(19,6): error CS0152: The label 'case 1:' already =
occurs in=20
this switch statement<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">z.abc(7) initializes =
i to 7,=20
hence<SPAN style=3D"mso-spacerun: yes"> </SPAN>the switch =
condition becomes=20
false. The case may show logical operations but finally has either 1 or =
0=20
standing for true or false. As the switch results in false, the first =
case is=20
executed where case has a value of 0. In the second case, z.abc(70), the =
switch=20
is true and thus the second case that evaluates to true gets called.=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">In one of the earlier =
examples,=20
we demonstrated that a duplicate case value was not admissible. The =
error=20
delivers the same message. In the above case, the compiler should take =
up the=20
Hitlerian attitude and erase all our code; to punish us for writing such =
gibberish. <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>}<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">The C# compiler =
informs us that a=20
switch statement can only take certain integral types. To be more =
specific, it=20
can take an sbyte, byte, short, ushort, int, uint, long, ulong, char, =
string, or=20
an enum-type. C# realizes that the switch has received a yyy object and =
since it=20
cannot convert a yyy into the above integral types, it reports an=20
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>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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -