Conditional break inside of switch case
I was wondering if it was 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 conditional is true I want to do something and then break out of the switch statement, but if not I just want to fall through to the next case.
Since you can't implicitly fall through to the next case, you must do it explicitly, using the goto
statement. 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 probably be better to refactor it using if
statements:
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;
}
No, C# doesn't allow fallthrough in switches, except if there is no code between the cases. See point 8.7.2 of the C# specification:
If the end point of the statement list of a switch section is reachable, a compile-time error occurs.
您可以转到其他案件与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;
}
链接地址: http://www.djcxy.com/p/84492.html
上一篇: 为什么Java中的“switch”依赖于“break”?
下一篇: 有条件的断开开关盒