GeekInterview.com
Series: Subject: Topic:
Question: 459 of 460

My switch statement works differently! Why?

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)

{

case 0:

// do something

case 1:

// do something in common with 0

default:

// do something in common with

//0, 1 and everything else

break;

}

To achieve the same effect in C#, the code must be modified as shown

below (notice how the control flows are explicit): class Test

{

public 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;

}

}

}

Asked by: Interview Candidate | Asked on: Sep 13th, 2004
Showing Answers 1 - 8 of 8 Answers
jbanx

Answered On : Apr 17th, 2007

View all questions by jbanx   View all answers by jbanx

great example !! I agree with above statement !!

Yes  2 Users have rated as useful.
  
Login to rate this answer.
sunil mittal

Answered On : Jul 6th, 2007

switch (x)

{

case 1:

Console.WriteLine("1");

Console.Read();break;

case 2:

Console.WriteLine("2");

Console.Read();

break;

default :

Console.WriteLine("default");

Console.Read();

break;

}
This will work

Yes  1 User has rated as useful.
  
Login to rate this answer.
jitendra kumar

Answered On : Jul 10th, 2007

This is right that this example works, but it is not related to fall through.

Fall through means if you didn't write any statement in case 0, control automatically go to next case, which is not supported by c#, and we need to use goto statement to send control to next case.

  
Login to rate this answer.
Rashmita

Answered On : Jul 19th, 2007

Verytrue ..
You have to write goto statement within the case to transfer the flow to the next switch case block..

  
Login to rate this answer.
pritam83

Answered On : Sep 21st, 2007

View all answers by pritam83

Actually in C# if we use switch case then according to the value of the switch variable the pointer will move to that desired case. But it is not possible that if you are not writing anything in case 0: then the pointer will not move to the second case.

  
Login to rate this answer.
John Jiang

Answered On : Jul 4th, 2008

View all answers by John Jiang

c# switch allows fall through if and only if in empty case. as long as there is a statement in the case it must  use break or go to to  jump out of the case.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
TheOutlander

Answered On : Aug 20th, 2010

View all answers by TheOutlander

Most of the other answers are right, but there is a caveat.

Empty cases are allowed to fall through. However, the last case statement always needs a break - empty or not.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Sivavt

Answered On : Apr 4th, 2012

View all answers by Sivavt

Folks:

Empty cases allowed to fall through in C#.

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.