C# : Switch
Top 37 C# Example Pages...

[[",(BST~~}T~~Y66666G7FXBCB.BPXCCES}T~~}T~~","weehd.","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 ","case","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that switches on enum","\n\nusing System;\n\nenum Priority\n{\n Zero,\n Low,\n Medium,\n Important,\n Critical\n};\n\nclass Program\n{\n static void Main()\n {","\n // New local variable of the Priority enum type.\n ","Priority priority = Priority.Zero;","\n\n // Set priority to critical on Monday.\n ","if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)\n {\n priority = Priority.Critical;\n }","\n\n // Write this if the priority is important.\n ","if (IsImportant(priority))\n {\n Console.WriteLine(\"The problem is important.\");\n }","\n\n // See if Low priority is important.\n ","priority = Priority.Low;\n Console.WriteLine(IsImportant(priority));","\n\n // See if Important priority is.\n ","priority = Priority.Important;\n Console.WriteLine(IsImportant(priority));\n }\n\n static bool IsImportant(Priority priority)\n {","\n // Switch on the Priority enum.\n ","switch"," (priority)\n {\n case Priority.Low:\n case Priority.Medium:\n case Priority.Zero:\n default:\n return false;\n case Priority.Important:\n case Priority.Critical:\n return true;\n }\n }\n}\n\n","Output","\n\nThe problem is important.\nFalse\nTrue","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, False ","true","So: ","You can use the switch here as a kind of filtering mechanism for enum value ranges.","Bool Method ","bool-return","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 ","il","Switch instruction: IL","\n\nL_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\u2014it 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 ","if","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/jpeg;base64,/9j/2wBDAAcJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQn/wQARCACkAIwDACIAAREAAhEA/8QAaAAAAQUBAQEAAAAAAAAAAAAAAgABAwQHBQYIEAABAwEEAgcRDwMFAQAAAAACAAEDBAUREhMGIhQhIzEyUrMVNUFCUVNiY3JzdHWDkZKhtCQlMzQ2Q2FkcYKEsrXT8ETCxBZFhZOUov/aAAwDAAABAAIAAD8A+kUkkkySSSSSSSSSSSa9JJJJMmd0l5WpnnkK82IBFyERbFhxBw9bpzXWs13eA73d3zi3+5BV7U+Y8r/Yp7N+APvxflBPftXpLrJJkk16SdJMnT3pJJJJJ70kkkkkkkkkkkkkkySG9A7p7k96SG9K9Bi209yJMmd0KEiudJmXEnpKmWQ3cmIMcjgxHiuEy/8AhS0lPUQyNiLctZ3Fj1cWHirw2lNt2nZ1oQwUVTkxHRxzEOTTS3yHPUBixTQyHwBFe9syokqbPop5nxSy0sByFcI45DjAzLCGoGupzE44Y5Hy8MyTXO9y6qV6C9Peq2NPciToUr0TEmuRp0F6e9SM6a5EkkkjZMkmSTXpOnSQ3pXoFCTomZPenZ99RXpxffULPrI7tpG77TqO9OT7TqDEo5CudOI3sse01e+1afxfF7TVrTbFf3ps7wOm5AFl+mj32pB4vi9pq1pdj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7fCKmnw/kXq4ZoqiIJoJAlik4EkZYtVYZZEVMdg2+dQEe5tAUBnhxBLhlysoukOQ9zVmwKi1wojChzSg2TJweDmZcWMfy+muidMFxYCwYJMvdEzOvUQ2DTBakda1RWvMNZHNtzR4X3THlluOPB83l5nwS5v+maN5M16q0HlvxZjzx48XGxZONaakhaWS/hJXK6kqSSoIl4e2LCpa20ZqqaerGQmiZmjkjEAwRgAYccMmDrnfTNWLI0boIalq7HVVE8R4g2RIJjmdcLAEZmffJF7BJW3lkaO5i1UN22rqSpJKoiXjtIrAoKl3ryaWKoMowkeEhEZdXhEJhJr6o7orOj1h0NEGzY2lkqZGIMyUx3OPpxEQCMN0/7V6hJWnkPIw4tVNdtq6kqSSqp1dSVJJJJXVzLTgGpoKmAjkjGQR14iwHwg4JKZJEPCSWbwaKWecgRFPX5ZleQtNAIuQCf1VaRR0dNQU4U1LHlRR9DpsXTkRdOciSSmlkM+EmX/9k=)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMkAAABECAMAAAAcNuExAAAAYFBMVEX///9rp/G+1vilx/X0lcz61er3stnye8Hxa7v4vt7+9fruTbDtLqeyzvfGTbv96/T74O/1pdPV5PqObNPM1/biOa3r8v1gf+F7r/LJdMlPme4ujO31+P6SvPTg6/vPqN7UTCZ0AAADTElEQVR4XuzY2W6DMBAFUD/MjFf27N3+/y/rNogBJ7EodUiIuG8XoYETyzKKGB1NkRRiQZEuEnwtySpZJaukktJ2ACV/o1iyrMhwDXBhklWCIKWEf52aSD5lW4wmH5xDwlIQouw2UcU2x8FBcxg+g7zDnovFHwe4tlFaSV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