This documentation is archived and is not being maintained.

Compiler Error CS0844

Visual Studio 2008

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.

To correct this error

  • 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;