Monday, 22 December 2008

switch statement auto-fill for enums in C#

Just a handy-dandy little tip for working with enums and switch statements in Visual Studio.

Let's say I have an enum defined somewhere:



public enum Genders { Female, Male, Neuter }



If I then declare a variable of the enumerated type:


...
Genders g;
...


And then create a switch statement using the switch code snippet (i.e. type the word “switch” and then press tab) I get the following:


switch (switch_on)
{
default:
}



and the word “switch_on” will be selected.

Change that switch_on to your enum variable’s name, in this case g, and then press the down-arrow cursor key (other keystrokes might work, but I know that down-arrow definitely does) and VS will automagically fill in the switch statement with the values:


switch (g)
{
case Genders.Female:
break;
case Genders.Male:
break;
case Genders.Neuter:
break;
default:
break;
}



Cool, eh?

2 comments:

lingmaaki said...

More about c# enum....C# enum

lig

raynnowui21 said...

Would you be curious about exchanging hyperlinks? best online casinos