Skip to main content

How to Resolve Error "CS0101: The namespace 'namespace' already contains a definition for 'type'" in C#

The Compiler Error CS0101 is a Naming Collision error. The message reads: "The namespace 'MyProject' already contains a definition for 'MyClass'".

In C#, every Type (Class, Struct, Enum, Interface, Delegate) must have a unique identifier within its specific namespace. The compiler uses the Fully Qualified Name (e.g., MyNamespace.MyClass) to locate types. If it scans your project and finds two definitions that map to the exact same Fully Qualified Name, it throws CS0101 because it cannot distinguish between them.

This guide explains the common causes of these duplicates and how to resolve them.

Understanding the Name Collision

The C# compiler compiles all files in a project into a single assembly. It does not care what your files are named; it only cares about the code inside them.

If you have:

  • File1.cs: namespace MyApp { class User { ... } }
  • File2.cs: namespace MyApp { class User { ... } }

The compiler sees MyApp.User defined twice. It stops because it doesn't know which version of User to use.

Scenario 1: Accidental File Duplication (Copy/Paste)

The most common cause is creating a backup of a file by copy-pasting it in the Solution Explorer or Windows Explorer (e.g., User.cs and User - Copy.cs), without changing the code inside the copy.

Example of error

You have two files. User.cs:

namespace MyApp
{
public class User
{
public int Id;
}
}

User_Backup.cs:

namespace MyApp
{
// ⛔️ Error CS0101: The namespace 'MyApp' already contains a definition for 'User'
// Even though the filename is different, the class name is the same.
public class User
{
public int Id;
}
}

Solution: Rename or Exclude

  1. Delete the duplicate file if it isn't needed.
  2. Exclude the backup file from the project (Right-click > Exclude from Project).
  3. Rename the class in the new file.
namespace MyApp
{
// ✅ Correct: Renamed the class to avoid collision
public class UserBackup
{
public int Id;
}
}

Scenario 2: Missing 'partial' Keyword

C# allows you to split a single class across multiple files using the partial keyword. This is standard in WinForms, WPF, and ASP.NET. If you create a second file for an existing class but forget the partial keyword, the compiler thinks you are trying to create a new class with the same name.

Example of error

File 1 (User.Props.cs):

namespace MyApp
{
public partial class User
{
public string Name { get; set; }
}
}

File 2 (User.Methods.cs):

namespace MyApp
{
// ⛔️ Error CS0101: Missing 'partial' here makes this a collision
// with the 'User' class in File 1.
public class User
{
public void Save() { }
}
}

Solution: Add 'partial'

Ensure both (or all) definitions of the class include the partial modifier.

namespace MyApp
{
// ✅ Correct: The compiler merges this with File 1.
public partial class User
{
public void Save() { }
}
}

Scenario 3: Different Types, Same Name

Type names must be unique regardless of the "Kind" of type. You cannot have a class and an enum with the same name in the same namespace.

Example of Mistake

namespace MyApp
{
public class Status
{
public bool IsActive { get; set; }
}

// ⛔️ Error CS0101: 'Status' is already used by the class above.
public enum Status
{
Active,
Inactive
}
}

Solution: Use Distinct Names

Rename one of the types to be more specific.

namespace MyApp
{
public class StatusResult
{
public bool IsActive { get; set; }
}

// ✅ Correct: Renamed to avoid conflict
public enum StatusCode
{
Active,
Inactive
}
}

Conclusion

CS0101 is a strict rule: One Name, One Definition.

  1. Check for Duplicates: Did you copy-paste a file and forget to rename the class inside?
  2. Check for Partials: Are you splitting a class? Make sure every file uses partial class ClassName.
  3. Check Conflicts: Ensure you don't have an Enum and a Class sharing the same name.