C#交换机标签systax

C#交换机标签systax,c#,switch-statement,C#,Switch Statement,案例标签的正确语法是什么。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

案例标签的正确语法是什么。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上从Microsoft下载的一些代码中有以下内容:

  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.

下载了我认为最新的版本,这是C#7中引入的新模式匹配语法。它基本上测试
NavigationRootPage.RootFrame?.Content是什么类型。例如,如果它是
ItemPage
,那么它的值被放入名为
ItemPage
的变量中。这很方便,因为您不必使用
is
as
操作符来检查每种类型和强制转换

您在语言规范中找不到,因为该规范的最新官方版本是针对C#5的,但是我还没有听说任何关于C#7的规范。如果您只想查看模式匹配语法的规范,可以按照Camilo Terevinto的建议找到建议


要使警告静音,请将
newControlsPage
allControlsPage
替换为

,这是C#7中引入的新模式匹配语法。它基本上测试
NavigationRootPage.RootFrame?.Content是什么类型。例如,如果它是
ItemPage
,那么它的值被放入名为
ItemPage
的变量中。这很方便,因为您不必使用
is
as
操作符来检查每种类型和强制转换

您在语言规范中找不到,因为该规范的最新官方版本是针对C#5的,但是我还没有听说任何关于C#7的规范。如果您只想查看模式匹配语法的规范,可以按照Camilo Terevinto的建议找到建议


要使警告静音,请将
newControlsPage
allControlsPage
替换为

我还可以补充一点,如果您认为我的答案回答了您的问题,可以使用
而不是
newControlsPage
allControlsPage
@user2825546来“改进”代码,请考虑通过点击这个标记接受它!我还可以补充说,代码可以通过使用<代码> < <代码>而不是<代码> NeXealPosiGe< /COD>和<代码> AutoPosiGe< <代码> @ USER 2525566,如果您认为我的答案回答了您的问题,请考虑通过单击该复选标记接受它。