Create switch with default values in CSharp

Description

The following code shows how to create switch with default values.

Example


/* ww w . j  av a 2  s. c  o m*/
using System;

public class MainClass {     
     static void Main()
     {
         const int Democrat = 0;
         const int Republican = 1;
         const int Progressive = 2;

         // hard wire to Republican
         int myChoice = 5;

         // switch on the value of myChoice
         switch (myChoice)
         {
             case Democrat:
                 Console.WriteLine("You voted Democratic.\n");
                 break;
             case Republican:
                 Console.WriteLine("You voted Republican.\n");
                 break;
             case Progressive:
                 Console.WriteLine("You voted Progressive.\n");
                 break;
             default:
                 Console.WriteLine("You did not make a valid choice.");
                 break;
         }
         Console.WriteLine("Thank you for voting.");
     }
 }

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception