Edit

Share via


Compiler Error CS0135

'declaration1' conflicts with the declaration 'declaration2'

The compiler does not allow hiding names, which commonly leads to logic errors in your code.

Example

The following sample generates CS0135:

C#
// CS0135.cs  
public class MyClass2  
{  
   public static int i = 0;  
  
   public static void Main()  
   {  
      {  
         int i = 4;  
         i++;  
      }  
      i = 0;   // CS0135  
   }  
}  

From the C# Language Specification:

It is an error for a local variable declaration space and a nested local variable declaration space to contain elements with the same name. Thus, within a nested declaration space it is not possible to declare a local variable or constant with the same name as a local variable or constant in an enclosing declaration space. It is possible for two declaration spaces to contain elements with the same name as long as neither declaration space contains the other.