Conditional gap inside the switch case

I was wondering if it is possible to conditionally break out of a case in a switch statement in C #. Take the following example.

MediaStream photoMediaStream = null; switch (photoSize) { case PhotoSize.Normal: if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth}); break; } case PhotoSize.Small: if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth}); break; } case PhotoSize.Thumb: if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth}); break; } } 

Basically, if the condition is true, I want to do something and then break out of the switch statement, but if not, I just want to move on to the next case.

+4
source share
8 answers

Since you cannot implicitly proceed to the next case, you must do this explicitly using the goto . This is one of the rare cases where the use of this statement is acceptable ...

 MediaStream photoMediaStream = null; switch (photoSize) { case PhotoSize.Normal: if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth}); break; } goto case PhotoSize.Small; case PhotoSize.Small: if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth}); break; } goto case PhotoSize.Thumb; case PhotoSize.Thumb: if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth}); break; } } 

Anyway, it would be better to reorganize it using if :

 MediaStream GetPhotoMediaStream(PhotoSize photoSize, /* whatever parameters you need... */) { if (photoSize == PhotoSize.Normal) { if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0) { return photoMedia.GetStream(new MediaOptions {Width = NormalWidth}); } photoSize = PhotoSize.Small; } if (photoSize == PhotoSize.Small) { if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0) { return photoMedia.GetStream(new MediaOptions {Width = SmallWidth}); } photoSize = PhotoSize.Thumb; } if (photoSize == PhotoSize.Thumb) { if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0) { return photoMedia.GetStream(new MediaOptions {Width = ThumbWidth}); } } return null; } 
+7
source

No, C # does not allow failure in switches, unless there is no code between cases. See clause 8.7.2 of the C # specification:

If the endpoint of the switch partition statement list is reachable, a compile-time error occurs.

+2
source

You can switch to another case with goto case .

 switch (photoSize) { case PhotoSize.Normal: if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth}); break; } goto case PhotoSize.Small; case PhotoSize.Small: if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth}); break; } goto case PhotoSize.Thumb; case PhotoSize.Thumb: if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth}); break; } break; } 
+2
source

C # does not support dropping through label labels when any code is between them (see passing operator instructions in C #? ).
Thus, of course, you can conditionally break - however, you will also have to break at the end of each block; -)

Just use some ifs.

+1
source

No, this is impossible, because if the if condition is not met, the program must go from one case to the next (and this is not allowed in C #).

From MSDN :

The execution of the list of statements in the selected section begins with the first statement and goes through the list of statements, as a rule, until the jump expression is reached , for example, break, goto case, return or quit . At this point, control is passed outside the switch statement or other case label.

Unlike C ++, C # does not allow you to continue execution from one switch section to the next .

+1
source

Well, if you need to provide for the execution of your code in a function, do not use the switch/case chain, but if . At that moment, when you want to "split" the flow of execution, use return .

0
source

As others have noted, C # doesn't let you fail. This allows you to stack cases, but it will not help you. However, you could achieve this by using goto outside the if condition to explicitly transfer control to another (or in your case, the next) case.

Before anyone says "OMG !! eww ... goto!", I must add that there are several cases that guarantee the use of gotos. This seems to be one of them. Until used incorrectly, nothing happens wrong with gotos.

0
source

Put a “gap” outside the condition of If ..

 MediaStream photoMediaStream = null; switch (photoSize) { case PhotoSize.Normal: if (imageWidth >= NormalWidth && imageWidth % NormalWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = NormalWidth}); } break; case PhotoSize.Small: if (imageWidth >= SmallWidth && imageWidth % SmallWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = SmallWidth}); } break; case PhotoSize.Thumb: if (imageWidth >= ThumbWidth && imageWidth % ThumbWidth == 0) { photoMediaStream = photoMedia.GetStream(new MediaOptions {Width = ThumbWidth}); } break; } 
0
source

Source: https://habr.com/ru/post/1401404/


All Articles