A little note about the switch statement in C #

Source: Internet
Author: User
Tags goto
Statement (first published in www.kunwsoft.com)
It is certainly not unfamiliar to the switch statement, which allows the program to choose from multiple actions based on the value of the control expression (If-else somewhat similar to the multiple-spoke statement, from a logical process). This is also true in C + + and Java, but in C #, there are some changes in this statement. Let's look at an example below:
switch (letter)
{
Case ' a ':
Console.WriteLine ("AAAAA");
Case ' B ':
Console.WriteLine ("BBBBB");
Break
}
This example in the "Case ' a" section will produce an error, because the program will always execute to "case" B ", which is a cross, but in C #, is prohibited through the occurrence of. Therefore, you must use the "break" "goto" or "return" to prevent the occurrence of the crossing. But the following is an exception:
switch (letter)
{
Case ' a ':
Case ' B ':
Console.WriteLine ("AAAAA");
Break
Case ' C ':
Console.WriteLine ("CCCCC");
Break
}
(The above situation, I believe, do not have to explain, can understand.) )
At this point, there is another problem, that is: In special circumstances, to enable the program to carry out such a cross, how to achieve? In fact, this is also very simple, the front is not mentioned, you can use the "goto" statement to prevent the program through, in fact, you can use it to achieve through, just use it to jump the program to another "case" not on the line!

Summary: in C + + and Java in the switch, allows the program to traverse, and C # is not allowed, however, you can use the "goto" statement disguised implementation.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.