GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 2 of 431    Print  

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;

}

}

}


  
Total Answers and Comments: 6 Last Update: July 04, 2008   
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
April 17, 2007 07:20:21   #1  
jbanx Member Since: February 2007   Contribution: 38    

RE: My switc...
great example !! I agree with above statement !!
 
Is this answer useful? Yes | No
July 06, 2007 06:23:30   #2  
sunil mittal        

RE: My switc...
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


 
Is this answer useful? Yes | No
July 10, 2007 02:02:40   #3  
jitendra kumar        

RE: My switc...

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.


 
Is this answer useful? Yes | No
July 19, 2007 13:59:02   #4  
Rashmita        

RE: My switc...
Very true ..
You have to write goto statement within the case to transfer the flow to the next switch case block..

 
Is this answer useful? Yes | No
September 21, 2007 01:39:04   #5  
pritam83 Member Since: September 2007   Contribution: 5    

RE: My switc...

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.


 
Is this answer useful? Yes | No
July 04, 2008 12:34:26   #6  
John Jiang Member Since: June 2008   Contribution: 8    

RE: My switch statement works differently! Why?
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.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape