You can do anything in a Case block inside a Switch/Case clause that you can do in
any method. Of course, you can't define a class, method, or struct in a Switch/Case block, just as you can't define those inside a standard method (unless you did some very esoteric stuff with reflection or Lambda Expressions, and so forth) ... but, why would you ever want to do that ?
Use of a Switch/Case clause is almost a standard in the classic Calculator example that many .NET books give: for example, if I create a set of Buttons, and set the text of each one of them to some basic calculation, and then
assign the same Click EventHandler to all of them:
private void CalcBtnClick(object sender, EventArgs e)
{
Button buttonClicked = sender as Button;
if(buttonClicked == null) throw new ArgumentException("argument passed to CalcBtnClick is not Type 'Button");
double arg1 = 10.0;
double arg2 = 20.0;
switch (buttonClicked.Text)
{
case "+":
tBoxResult.Text = (arg1 + arg2).ToString();
break;
case "-":
tBoxResult.Text = (arg1 - arg2).ToString();
break;
case "*":
tBoxResult.Text = (arg1 * arg2).ToString();
break;
}
}
There is a more "elegant" way to do this using a Dictionary holding Buttons as Keys, and Funcs (methods ... in the form of Lambda Expressions) as Values, but it is a very advanced use of .NET.
On request I will demonstrate that here.