null-string

C# 7 switch case with null checks

淺唱寂寞╮ 提交于 2019-12-08 21:46:01
问题 C#7 introduces a new feature called patterns , which you can use with Is-Expression or Switch cases like this: string str = null; switch(str){ case string x: Console.WriteLine("string " + x); break; default: Console.WriteLine("default"); break; } and you would expect that it will goes inside case #1, as it is the same type, but it didn't. 回答1: Despite what you might think, string x = null actually isn't a string at all. It is 'nothing', assigned to a variable of type string. The check in your