编译器错误 CS0103

更新:2007 年 11 月

错误消息

当前上下文中不存在名称“identifier”

试图使用类、命名空间或范围中不存在的名称。检查名称的拼写,并检查 using 语句和程序集引用,确保您尝试使用的名称可用。

如果在循环或者 try 或 if 块中声明一个变量,然后试图从封闭代码块或另一代码块访问此变量,会发生此错误。如下面的示例所示。

下面的示例生成 CS0103:

// CS0103.cs
using System;

class MyClass
{
   public static void Main()
   {
      // MyClass conn = null;
      try
      {
         MyClass conn = new MyClass();   // delete this line
         // and uncomment the following line and the line above the try
         // conn = new MyClass();
      }
      catch(Exception e)
      {
         if (conn != null)   // CS0103
            Console.WriteLine("{0}", e);
      }
   }
}