[Import] Why does my switch statement works differently?

Source: Internet
Author: User

C # does not support an explicit fall through for case blocks (unless the block is empty)

For an explanation of why, see why is the C # Switch statement designed to not allow fall through, but still require a break? On msdn

The following code is not legal and will not compile in C #:

Switch (X)

{

Case 0:

Console

. Writeline (X)

// Do something

Case 1:

Console

. Writeline (X)

// Do something in common with 0

Default:

Console

. Writeline (X)

// Do something in common with 0, 1 and everything else

Break;

}

 
In order to achieve the same effect in C # The code must be modified as shown below (Notice how the control flows are very explicit ):

  

Class

Test

{

Static void main ()

{

Int x = 3;

 

Switch (X)

{

Case 0:

// Do something

Goto case 1;

Case 1:

// Do something in common with 0

Goto default;

Default:

// Do something in common with 0, 1, and anything else

Break;

}

}

}


Source: http://blogs.msdn.com/csharpfaq/archive/2004/07/22/191787.aspx

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.