Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Cannot use local variable 'name' before it is declared. The declaration of the local variable hides the field 'name'.
An identifier can have only one meaning in a given block. Local variables that have the same name as class fields can hide the field by introducing a second meaning for the identifier. Therefore the compiler generates an error when you refer to a class field in a method, and then declare a local variable by the same name.
Use
this.num
to refer to the class field.Give the local variable a different name from the class field.
The following code generates CS0844:
class Test
{
int num;
public void TestMethod()
{
num = 5; // CS0844
int num = 6;
}
public static int Main()
{
return 1;
}
}
class Test
{
int num;
public void TestMethod()
{
this.num = 5; // Error fixed.
int num = 6;
}
public static int Main()
{
return 1;
}
}
class Test
{
int num;
public void TestMethod()
{
num = 5; // Error fixed.
int num2 = 6;
}
public static int Main()
{
return 1;
}
}
.NET feedback
.NET is an open source project. Select a link to provide feedback: