Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will a C# switch statement's default label handle a nullable enum?

How will a C# switch statement's default label handle a nullable enum?

Will the default label catch nulls and any unhandled cases?

like image 422
Trey Mack Avatar asked Feb 19 '13 05:02

Trey Mack


People also ask

Will Ab raise my GPA?

Only grades above a C can raise your GPA to a 2.0- Every D requires a B and every F requires an A to achieve a 2.0 overall.


2 Answers

If it's null, it will hit the default label.

public enum YesNo {     Yes,     No, }  public class Program {     public static void Main(string[] args)     {         YesNo? value = null;         switch (value)         {             case YesNo.Yes:                 Console.WriteLine("Yes");                 break;             case YesNo.No:                 Console.WriteLine("No");                 break;             default:                 Console.WriteLine("default");                 break;         }     } } 

The program will print default.

Unless null is handled.

public class Program {     public static void Main(string[] args)     {         YesNo? value = null;         switch (value)         {             case YesNo.Yes:                 Console.WriteLine("Yes");                 break;             case YesNo.No:                 Console.WriteLine("No");                 break;             case null:                 Console.WriteLine("NULL");                 break;             default:                 Console.WriteLine("default");                 break;         }     } } 

prints NULL.

If you have an unhandled enum value that was added later:

public enum YesNo {     Yes,     No,     FileNotFound, }  public class Program {     public static void Main(string[] args)     {         YesNo? value = YesNo.FileNotFound;         switch (value)         {             case YesNo.Yes:                 Console.WriteLine("Yes");                 break;             case YesNo.No:                 Console.WriteLine("No");                 break;             default:                 Console.WriteLine("default");                 break;         }     } } 

It still prints default.

like image 148
Trey Mack Avatar answered Sep 24 '22 21:09

Trey Mack


You can use the null-coalescing operator ?? to route null switch values to a specific case label other than default:

public static IEnumerable<String> AsStrings(this IEnumerable<Char[]> src) {     Char[] rgch;      var e = src.GetEnumerator();     while (e.MoveNext())     {         switch ((rgch = e.Current)?.Length ?? -1)         {             case -1:    // <-- value when e.Current is 'null'                 yield return null;                 break;             case 0:                 yield return String.Empty;                 break;             case 1:                 yield return String.Intern(new String(rgch[0], 1));                 break;             default:   // 2...n                 yield return new String(rgch);                 break;         }     } } 
like image 25
Glenn Slayden Avatar answered Sep 21 '22 21:09

Glenn Slayden