[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Switch differences between C++ and C#

Posted on 2006-06-07
12
Medium Priority
?
425 Views
Last Modified: 2010-04-16
I know that using break statement like in C++ works, but what is the intended way of writing a switch statement in C#? If I don't want any fall-through, do I need any break at all? Only at the end of switch statement?

Thanks
0
Comment
Question by:gromul
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16854823
You can't have fall-through in C#.  C# forces you to break between cases, unless you're handling multiple cases with the same block:

switch (myValue)
{
  case 1:
   // do stuff
   break;
  case 2:
    // do stuff
    // compiler will error here as you must break from this case before moving to the next one
  case 3:
  case 4:
   // do stuff
   // this is legal since case 3 and case 4 are both handled here
   break;
}
0
 

Author Comment

by:gromul
ID: 16854827
I already found the first of these pages. So why do I need break in the default case?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16854849
>So why do I need break in the default case?
because C# rules it like that. to protect against weak programming...
0
 
LVL 11

Expert Comment

by:vo1d
ID: 16854853
its like dstanley9 , c# does not allow any fall-throughs, which would be one missing break in the default case.
0
 

Author Comment

by:gromul
ID: 16854854
My understanding is, C++ forces you to break between cases if you don't want fall through. C# doesn't have fall through, unless explicitate it with goto statements:

switch (i) {
   case 0:
      CaseZero();
      goto case 1;
   case 1:
      CaseZeroOrOne();
      goto default;
   default:
      CaseAny();
      break;
}
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 800 total points
ID: 16854881
>C# doesn't have fall through, unless explicitate it with goto statements:
well, in fact that is what C# wants you do to, so you KNOW (you are aware) that you WANT fallthrough, and did not simply forget to put the break statement,
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16854882
That is correct.  Like AngelIII said, it's to protect against unintended fall-through.
0
 

Author Comment

by:gromul
ID: 16854900
I see. Thanks
0
 

Author Comment

by:gromul
ID: 16854933
If I use the default case for errors and return, say, -1, then I don't need the break statement in the default case, right?
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 1200 total points
ID: 16854939
If you return you do not need a break because you have explicitly prevented fall-through.
0
 

Author Comment

by:gromul
ID: 16855000
Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Loops Section Overview
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question