【问题标题】:odd variable scope in switch statementswitch 语句中的奇变量范围
【发布时间】:2012-11-23 08:10:41
【问题描述】:

This question 让我想起了一个关于 switch 的老问题:

    int personType = 1;
    switch (personType)
    {
        case 1:
            Employee emp = new Employee();
            emp.ExperienceInfo();
            break;
        case 2:
            Employee emp = new Employee(); 
            //Error: A local variable named 'emp' is already defined in this scope
            emp.ManagementInfo();
            break;
        case 3:
            Student st = new Student();
            st.EducationInfo();
            break;
        default:
            MessageBox.Show("Not valid ...");
    }

为什么 emp 在“案例 2”中被识别?在 C++ 中(如果我没记错的话)我们可以同时使用多个案例,但在 C# 中这是不可能的,我们应该用 break 关闭 case 1,所以下面的代码在 C++ 中似乎是正确的,而在 C# 中是错误的:

case 1:
case 2:
   SomeMethodUsedByBothStates();

当我们不能有这样的行为时,为什么我们应该能够在case 1 中声明 emp 并在case 2 中看到它?如果从来没有两种情况同时发生,那为什么要同时看到对象呢?

【问题讨论】:

    标签: c# c++ scope switch-statement


    【解决方案1】:

    在 c++ 或 c# 中,案例不会创建范围。在 case 中声明的所有这些变量都在相同的范围内,即 switch 语句的范围内。如果您希望这些变量在某些特定情况下是本地的,则需要使用大括号:

    switch (personType)
    {
        case 1: {
            Employee emp = new Employee();
            emp.ExperienceInfo();
            break;
        }
        case 2: {
            Employee emp = new Employee(); 
            // No longer an error; now 'emp' is local to this case.
            emp.ManagementInfo();
            break;
        }
        case 3: {
            Student st = new Student();
            st.EducationInfo();
            break;
        }
        ...
    }
    

    【讨论】:

      【解决方案2】:

      假设案例 2 有 breakreturn,您展示的第二个代码在 C# 中非常好:

      case 1:
          // no code here...
      case 2:
          SomeMethodUsedByBothStates();
          break;
      

      允许空箱掉线。
      not 允许的是在一个失败的 case-branch 中有代码。因此,以下内容将无效:

      case 1:
          SomeMethodUsedOnlyByCase1();
          // no break here...
      case 2:
          SomeMethodUsedByBothStates();
          break;
      

      关于范围的问题是另一个问题。 基本上,范围是 switch 语句本身,而不是 case-branch。

      要使您的示例编译,只需通过添加花括号来为 case-branch 提供它们自己的范围:

      int personType = 1;
      switch (personType)
      {
          case 1:
          {
              Employee emp = new Employee();
              emp.ExperienceInfo();
              break;
          }
          case 2:
          {
              Employee emp = new Employee(); 
              emp.ManagementInfo();
              break;
          }
          case 3:
          {
              Student st = new Student();
              st.EducationInfo();
              break;
          }
          default:
              MessageBox.Show("Not valid ...");
      }
      

      【讨论】:

        【解决方案3】:

        在案例中声明变量时,请使用大括号进行说明。

        int personType = 1;
        switch (personType)
        {
            case 1: 
           {
             ///
             break;
           }
            case 2: 
           {
             ///
             break;
            }
            case 3: 
           {
                ///
                break;
           }
            ...
        }
        

        【讨论】:

          【解决方案4】:

          范围一点也不奇怪。局部变量的范围是从定义它的点到定义它的块的末尾。因此,各种emp 变量都在范围内,直到在switch 语句之后以{ 开始并在相应的} 结束的块结束。 case 标签没有什么特别之处;它们不会改变变量的范围。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-12
            • 1970-01-01
            • 2020-11-09
            • 1970-01-01
            相关资源
            最近更新 更多