הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Cannot use local variable 'name' before it is declared.
A variable must be declared before it is used.
The following example generates CS0841:
// cs0841.cs
using System;
public class Program
{
public static void Main()
{
j = 5; // CS0841
int j;
}
}
Move the variable declaration before the line where the error occurs.
using System;
public class Program
{
public static void Main()
{
int j;
j = 5;
}
}
In the following example, the intent was comparing parameter
with MyEnum.A
. Because a local variable declared later with the same type name, it shadows the type MyEnum
and MyEnum
in this method no longer refers to the enum
, but refers to the declared local variable.
using System;
public enum MyEnum
{
A, B, C
}
public class C
{
public void M(MyEnum parameter)
{
// error CS0841: Cannot use local variable 'MyEnum' before it is declared
if (parameter == MyEnum.A)
{
return;
}
// Change the variable to 'myEnum' to avoid shadowing the 'MyEnum' type.
// This change also aligns with the C# coding conventions.
var MyEnum = parameter;
Console.WriteLine(MyEnum.ToString());
}
}
משוב של .NET
.NET הוא פרויקט קוד פתוח. בחר קישור כדי לספק משוב: