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()
{
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for