C#switch-label systax

use*_*546 0 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:
switch-label
switch-labels   switch-label
switch-label:
case   constant-expression   :
default   :
Run Code Online (Sandbox Code Playgroud)

所以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;
  }
Run Code Online (Sandbox Code Playgroud)

在resharper中,它表示newControlPage是一个从未使用过的变量.

所以c#规格也不正确.

我刚下载了我认为是MS的最新版本.

Swe*_*per 5

这是C#7中引入的新模式匹配语法.它基本上测试了什么类型NavigationRootPage.RootFrame?.Content.如果是ItemPage,例如,则将其值放入名为的变量中itemPage.这很方便,因为您不必使用isas运算符来检查每种类型和强制类型.

你不会在语言规范中找到它,因为该规范的最新官方版本是针对C#5的.我知道他们正在为C#6起草一个规范,但我没有听说过C#7的规范.如果你只是想查看模式匹配语法的规范,可以在这里找到提议,正如Camilo Terevinto所建议的那样.

要使警告静音,请更换newControlsPageallControlsPage使用_.