Skip to main content

How to Resolve Error "CS0102: The type 'type name' already contains a definition for 'identifier'" in C#

The Compiler Error CS0102 is a Naming Collision error within a class or struct. The message reads: "The type 'MyClass' already contains a definition for 'MemberName'".

In C#, every member (field, property, method, event, or nested type) within a specific scope (like a class) must have a unique signature. While C# allows Method Overloading (methods with the same name but different parameters), it does not allow two members to share the exact same identifier if they are fields, properties, or methods with identical signatures.

This guide explains how to identify and resolve these duplicate definitions.

Understanding Member Scope

When you define a class, the compiler creates a symbol table for it. Every identifier you add must be unique enough for the compiler to distinguish it.

  • Allowed: void Run() and void Run(int speed) (Different signatures).
  • Allowed: int Count and int count (Different casing - C# is case-sensitive).
  • Not Allowed: int Status and string Status (Different types do not make the name unique).
  • Not Allowed: int Data (Field) and void Data() (Method).

Scenario 1: Duplicate Fields or Properties

The most basic cause is accidentally declaring the same variable twice. This often happens after copy-pasting code or merging changes from version control.

Example of error

public class User
{
public int Id { get; set; }
public string Name { get; set; }

// ⛔️ Error CS0102: The type 'User' already contains a definition for 'Name'
// You cannot declare 'Name' again, even if the type is different or it's a field.
public string Name;
}

Solution: Rename or Remove

Choose a unique name for the second member.

public class User
{
public int Id { get; set; }
public string Name { get; set; }

// ✅ Correct: Renamed to avoid collision
public string Nickname;
}

Scenario 2: Property vs. Method Collisions

A common mistake—especially for developers coming from Java or C++—is naming a method exactly the same as a property. In C#, this is forbidden because the syntax obj.Name (Property) and obj.Name() (Method) is too similar in terms of member lookup mechanics.

Example of error

public class DataProcessor
{
public string Status { get; set; }

// ⛔️ Error CS0102: 'DataProcessor' already contains a definition for 'Status'
// You cannot have a Property 'Status' and a Method 'Status' in the same class.
public void Status()
{
System.Console.WriteLine(this.Status);
}
}

Solution: Rename the Method

Use a verb for the method to distinguish it from the noun (Property).

public class DataProcessor
{
public string Status { get; set; }

// ✅ Correct: Renamed to 'PrintStatus' (or 'GetStatus')
public void PrintStatus()
{
System.Console.WriteLine(this.Status);
}
}

Scenario 3: Partial Classes (Hidden Duplicates)

When using Partial Classes (common in WinForms, WPF, ASP.NET, or Entity Framework), the class definition is split across multiple files. You might define a member in MyClass.cs that already exists in MyClass.Designer.cs or an auto-generated file.

File 1: User.cs

public partial class User
{
public int Id { get; set; }
}

File 2: User.Generated.cs

public partial class User
{
// ⛔️ Error CS0102: 'Id' is already defined in User.cs
public int Id { get; set; }
}

Solution

Check the other files belonging to that class. Since partial classes are merged into one single class during compilation, you only need to define the member once across all files.

Conclusion

CS0102 enforces strict uniqueness for identifiers within a type.

  1. Check Names: Ensure no two members share the exact same name.
  2. Check Types: Remember that changing the return type (e.g., int vs string) does not resolve a name collision.
  3. Check Partials: If the class is partial, look in other files to see if the member is already defined there.