Sunday, March 29, 2015

My switch statement works differently! Why?

Question:My switch statement works differently! Why??

Answer: </P><P align=justify>C# does not support an explicit fall through for case blocks. The following code is not legal and will not compile in C#: switch(x)</P><P align=justify>{</P><P align=justify>case 0:</P><P align=justify>// do something</P><P align=justify>case 1:</P><P align=justify>// do something in common with 0</P><P align=justify>default:</P><P align=justify>// do something in common with</P><P align=justify>//0, 1 and everything else</P><P align=justify>break;</P><P align=justify>}</P><P align=justify></P><P align=justify>To achieve the same effect in C#, the code must be modified as shown </P><P align=justify>below (notice how the control flows are explicit): class Test</P><P align=justify>{</P><P align=justify>public static void Main()</P><P align=justify>{</P><P align=justify>int x = 3;</P><P align=justify></P><P align=justify>switch(x)</P><P align=justify>{</P><P align=justify>case 0:</P><P align=justify>// do something</P><P align=justify>goto case 1;</P><P align=justify>case 1:</P><P align=justify>// do something in common with 0</P><P align=justify>goto default;</P><P align=justify>default:</P><P align=justify>// do something in common with 0, 1, and anything else</P><P align=justify>break;</P><P align=justify>}</P><P align=justify>}</P><P align=justify>}

Tags:Interview Questions, Microsoft, C#,

Please submit your Interview Answer for the above Question via replying to this post. Thank you for visiting to our blog, Have a Good Day!.



Sponsor Advertisement




Medical Billing Software

Hospital Management System Software

Hospital Management System (HMS) Software is developed for multispeciality hospitals, to cover a wide range of hospital administration and management processes with financial accounting, voucher entry, pharmacy, patients module etc features...

No comments:

Post a Comment