Home > C# > [C#] Using the switch case Statement

[C#] Using the switch case Statement

Saturday, 26 April, 2008 Leave a comment Go to comments

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;

Categories: C# Tags: ,
  1. Monday, 28 April, 2008 at 12:56 pm

    Тағы бір мысал:

    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”:
    if (ValidateFileDimensions())
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string saveAsName = Path.Combine(Server.MapPath(“~/Uploads/”), fileName);
    lblSucces.Visible = true;
    valInvalidDimensions.IsValid = false;
    valInvalidDimensions.ErrorMessage = String.Format(valInvalidDimensions.ErrorMessage, height, width);
    // Unable to read the file dimensions. The uploaded file is probably not an image.
    valInvalidFile.IsValid = false;
    default: // The uploaded file has an incorrect extension
    valInvalidFile.IsValid = false;

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: