How will a C# switch statement's default label handle a nullable enum?
Will the default label catch nulls and any unhandled cases?
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.
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
.
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; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With