TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Switch Enum

This C# example uses switch on enum variables. It uses cases with enum values.

Switch can act upon enum values.

An enum switch sometimes results in clearer code. It sometimes is faster. This code pattern is often effective. It is used in many real-world programs, not just website examples.

Example. Enums are useful in your program if it has to use magic constants. For the switch statement, look at the IsImportant method defined at the bottom of this example. It uses five explicit cases and a default case.

Case

C# program that switches on enum

using System;

enum Priority
{
    Zero,
    Low,
    Medium,
    Important,
    Critical
};

class Program
{
    static void Main()
    {
	// New local variable of the Priority enum type.
	Priority priority = Priority.Zero;

	// Set priority to critical on Monday.
	if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
	{
	    priority = Priority.Critical;
	}

	// Write this if the priority is important.
	if (IsImportant(priority))
	{
	    Console.WriteLine("The problem is important.");
	}

	// See if Low priority is important.
	priority = Priority.Low;
	Console.WriteLine(IsImportant(priority));

	// See if Important priority is.
	priority = Priority.Important;
	Console.WriteLine(IsImportant(priority));
    }

    static bool IsImportant(Priority priority)
    {
	// Switch on the Priority enum.
	switch (priority)
	{
	    case Priority.Low:
	    case Priority.Medium:
	    case Priority.Zero:
	    default:
		return false;
	    case Priority.Important:
	    case Priority.Critical:
		return true;
	}
    }
}

Output
    (First line is only written on Monday.)

The problem is important.
False
True

This program defines a custom method that contains a switch, which tests the parameter that is an enum of type Priority. It returns true if the Priority value is Important or Critical. Otherwise it returns false.

True and False

So: You can use the switch here as a kind of filtering mechanism for enum value ranges.

Bool Methods, Return True and False

Internals. How is the enum switch compiled? The above C# code is compiled into a special .NET Framework instruction called a jump table. The jump table uses the IL instruction switch. It defines one jump "point" to each case.

IL: switch

Switch instruction: IL

L_0004: switch (L_001f, L_001f, L_001f, L_0023, L_0023)

Summary. We saw examples of how you can switch on enums in the C# language. Switch can improve performance when it is implemented by a jump table in the MSIL. This is determined by the compiler.

Therefore: When you have a set of constants, I recommend you use switch—it can have better performance. And the code is clearer to read.

Also: An if-statement can always replace a switch. Developers are often more familiar with if-statements.

If


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf