Friday 20 November 2015

The switch statement in asp.net

Introduction:

 I will explain here what switch case is and how to use in asp.net with c# code.

Explanation:

The switch...case statement is good for selecting one branch of execution from a set of mutually exclusive ones.

It takes the form of a switch argument followed by a series of case clauses. When the expression in the switch argument evaluates to one of the values beside a case clause, the code immediately following the case clause executes. This is one example where we don’t need to use curly braces to join statements into blocks; instead, we mark the end of the code for each case using the break statement. We can also
include a default case in the switch statement, which will execute if the expression evaluates to none of the other cases. The following switch statement tests the value of the integer “A” variable:


switch (integer A)
{
case 1:
Console.WriteLine(“integer A is 1 ”);
break;
case 2:
Console.WriteLine(“integer A is 2 ”);
break;
case 3:
Console.WriteLine(“integer A is 3 ”);
break;
default:
Console.WriteLine(“integer A is not 1,2, or 3”);
break;
}

Note that the case values must be constant expressions; variables are not permitted.
Though the switch...case statement. If a case clause is fired early on in the block, later clauses cannot be fired unless you use a goto statement to mark that you want them fired too.

Control cannot fall through from one case label (‘case 2:’) to another While it is true that fall-through behavior is desirable in a limited number of situations, in the vast
majority of cases it is unintended and results in a logical error that’s hard to spot. Isn’t it better to code for the norm rather than for the exception? By getting creative with goto statements (which C# does support) however, you can duplicate fall through functionality in your switch...cases. However, if you find yourself really wanting to, you probably should re-consider your approach. The following code illustrates both how to use goto to simulate fall-through, and how messy the resultant code can get.

// assume country and language are of type string
switch(country)
{
case “America”:
CallAmericanOnlyMethod();
goto case “Britain”;
case “France”:
language = “French”;
break;
case “Britain”:
language = “English”;
break;
}

There is one exception to the no–fall-through rule however, in that we can fall through from one case to the next if that case is empty. This allows us to treat two or more cases in an identical way (without the need for goto statements):

switch(country)
{
case “au”:
case “uk”:
case “us”:
language = “English”;
break;
case “at”:
case “de”:
language = “German”;
break;
}

One intriguing point about the switch statement in C# is that the order of the cases doesn’t matter—we can even put the default case first! As a result, no two cases can be the same. This includes different constants that have the same value, so we can’t, for example, do this:

// assume country is of type string
const string england = “uk”;
const string britain = “uk”;
switch(country)
{
case england:
case britain: // this will cause a compilation error
language = “English”;
break;
}

No comments:

Post a Comment