Compiler Error CS0542

'user-defined type' : member names cannot be the same as their enclosing type

The members of a class or struct cannot have the same name as the class or struct, unless the member is a constructor.

The following sample generates CS0542:

// CS0542.cs  
class C  
{  
    public int C;  
}  

This error might be caused if you inadvertently put a return type on a constructor, which in effect makes it into an ordinary method. The following example generates CS0542 because F is a method, not a constructor, because it has a return type:

// CS0542.cs  
class F  
{  
   // Remove void from F() to resolve the problem.  
   void F()   // CS0542, same name as the class  
   {  
   }  
}  
  
class MyClass  
{  
   public static void Main()  
   {  
   }  
}  

If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict.

// CS0542b.cs  
class Item  
{  
   public int this[int i]  // CS0542  
   {  
      get  
      {  
         return 0;  
      }  
   }  
}  
  
class CMain  
{  
   public static void Main()  
   {  
   }  
}