編譯器錯誤 CS0103
目前的內容中沒有名稱 'identifier'
嘗試使用類別、命名空間或範圍中沒有的名稱。 請檢查名稱的拼字,以及您的 using 指示詞和組件參考,以確定可使用您嘗試使用的名稱。
如果您在迴圈、try
或 if
區塊中宣告一個變數,然後嘗試從封入程式碼區塊或另一個程式碼區塊存取此變數,就會經常發生這種錯誤,如下列範例所示:
注意
在 Lambda 運算式中遺漏 =>
運算子的 greater than
符號時,也可能會顯示此錯誤。 如需詳細資訊,請參閱 Lambda 運算式。
using System;
class MyClass1
{
public static void Main()
{
try
{
// The following declaration is only available inside the try block.
var conn = new MyClass1();
}
catch (Exception e)
{
// The following expression causes error CS0103, because variable
// conn only exists in the try block.
if (conn != null)
Console.WriteLine("{0}", e);
}
}
}
下列範例將解決此錯誤:
using System;
class MyClass2
{
public static void Main()
{
// To resolve the error in the example, the first step is to
// move the declaration of conn out of the try block. The following
// declaration is available throughout the Main method.
MyClass2 conn = null;
try
{
// Inside the try block, use the conn variable that you declared
// previously.
conn = new MyClass2();
}
catch (Exception e)
{
// The following expression no longer causes an error, because
// the declaration of conn is in scope.
if (conn != null)
Console.WriteLine("{0}", e);
}
}
}
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應