case标签的正确语法是什么。C#规范中写道:
switch-statement:
switch ( expression ) switch-block
switch-block:
{ switch-sectionsopt }
switch-sections:
switch-section
switch-sections switch-section
switch-section:
switch-labels statement-list
switch-labels:
switch-label
switch-labels switch-label
switch-label:
case constant-expression :
default :因此,case语句是'case‘后跟一个常量,后跟一个:。然而,在我从微软下载的GitHub上的一些代码中,它有以下内容:
switch (NavigationRootPage.RootFrame?.Content)
{
case ItemPage itemPage:
itemPage.SetInitialVisuals();
break;
case NewControlsPage newControlsPage:
case AllControlsPage allControlsPage:
NavigationRootPage.Current.NavigationView.AlwaysShowHeader = false;
break;
}在resharper中,它指出newControlPage是一个从未使用过的变量。
所以c#规范是不正确的吗?
我刚从MS.下载了我认为是最新版本的东西。
发布于 2018-08-03 08:44:49
这是C# 7中引入的新模式匹配语法。它主要测试NavigationRootPage.RootFrame?.Content是什么类型。例如,如果它是ItemPage,那么它的值被放入一个名为itemPage的变量中。这很方便,因为您不必使用is和as操作符来检查每个类型和类型转换。
你不会在语言规范中找到这一点,因为该规范的最新官方发布是针对C# 5. I know they are drafting a spec for C# 6的,但我还没有听说过任何关于C# 7的规范。如果你只想看看模式匹配语法的规范,这个建议可以在here找到,正如Camilo Terevinto所建议的那样。
要使警告静默,请将newControlsPage和allControlsPage替换为_。
https://stackoverflow.com/questions/51663883
复制相似问题