[C#] Using the switch case Statement
The switch statement chooses flow of control based on the evaluation of a
numeric or string comparison.The switch statement does not allow control to fall
through to the next case as in C/C++ unless the case statement is followed
immediately by another case statement. In other words, you must use a break statement
with every case statement.You can also use a goto statement, although most
programmers frown on using them. Here are two examples:
int j = 0;
int i = 1;
switch (i)
{
case 1:j = 7;break;
case 2:
case 3:j = 22;break;
default:j = 33; break;
}
…
string lastName = “”;
string text = “fred”;
switch ( text )
{
case “fred”:lastName = “Flinstone”;break;
case “barney”:lastName = “Rubble”;break;
default:lastName = “Slate”;break;
}

Тағы бір мысал:
if (FileUpload1.HasFile)
{
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
switch (extension.ToLower())
{
// Only allow uploads that look like images.
case “.jpg”:
case “.jpeg”:
case “.gif”:
case “.bmp”:
try
{
if (ValidateFileDimensions())
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string saveAsName = Path.Combine(Server.MapPath(“~/Uploads/”), fileName);
FileUpload1.PostedFile.SaveAs(saveAsName);
lblSucces.Visible = true;
}
else
{
valInvalidDimensions.IsValid = false;
valInvalidDimensions.ErrorMessage = String.Format(valInvalidDimensions.ErrorMessage, height, width);
}
}
catch
{
// Unable to read the file dimensions. The uploaded file is probably not an image.
valInvalidFile.IsValid = false;
}
break;
default: // The uploaded file has an incorrect extension
valInvalidFile.IsValid = false;
break;
}
}