How to Resolve Error "CS0434: The namespace 'Name' in 'Namespace' conflicts with the type 'Name' in 'Namespace'" in C#
The Compiler Error CS0434 is a Cross-Assembly Naming Conflict. The message reads: "The namespace 'Name1' in 'NamespaceA' conflicts with the type 'Name1' in 'NamespaceB'".
This error occurs when you reference two different assemblies (DLLs) in your project, and they define identifiers that overlap in a specific way. Specifically, Assembly A defines a Namespace (e.g., MyLib.Data), and Assembly B defines a Type (Class/Struct) with the exact same fully qualified name (e.g., MyLib.Data).
When you try to use MyLib.Data in your code, the compiler cannot determine if you are referring to the namespace container from Assembly A or the class from Assembly B.
This guide explains how to resolve this ambiguity using Extern Aliases or code refactoring.
Understanding the Namespace-Type Conflict
In C#, Namespaces and Types are distinct constructs, but they share the same symbol table for resolution.
- Assembly A: Contains
namespace Utils.Log { ... } - Assembly B: Contains
namespace Utils { public class Log { ... } }
If you reference both assemblies, typing Utils.Log is ambiguous. The compiler sees two valid candidates imported from the global namespace, and standard resolution rules cannot break the tie because they come from different physical files (assemblies).
Scenario: The Collision
Imagine you are building a project that references two third-party libraries: Grid.Layout.dll and Grid.Core.dll.
Code in Grid.Layout.dll:
namespace Grid.UI
{
// A nested namespace
namespace Controls
{
public class Button { }
}
}
Code in Grid.Core.dll:
namespace Grid.UI
{
// A class named 'Controls'
public class Controls
{
public int Count;
}
}
Your Consumer Project:
using Grid.UI;
public class Program
{
static void Main()
{
// ⛔️ Error CS0434: The namespace 'Grid.UI.Controls' in 'Grid.Layout.dll'
// conflicts with the type 'Grid.UI.Controls' in 'Grid.Core.dll'.
// Are we accessing the Class 'Controls'?
// Or are we trying to reach the Namespace 'Controls'?
var x = new Grid.UI.Controls();
}
}
Solution 1: Use extern alias (The Robust Fix)
If you cannot change the source code of the libraries (e.g., they are NuGet packages), you must use the C# Extern Alias feature. This allows you to give a specific assembly a unique root name, separating it from the global namespace.
Step 1: Assign an Alias in Visual Studio
- Go to Solution Explorer > Dependencies > Assemblies.
- Right-click the reference causing the conflict (e.g.,
Grid.Layout.dll). - Select Properties.
- Find the Aliases property. Change
globalto a custom name, e.g.,LayoutLib.
(Alternatively, in your .csproj file):
<ItemGroup>
<Reference Include="Grid.Layout">
<Aliases>LayoutLib</Aliases>
</Reference>
</ItemGroup>
Step 2: Use the Namespace Alias Qualifier (::)
In your code, you must explicitly declare that you want to use the alias, and then use the double-colon :: operator to access the root of that specific assembly.
// 1. Declare the alias usage at the top of the file
extern alias LayoutLib;
using System;
public class Program
{
static void Main()
{
// ✅ Correct: Accessing the Namespace from the aliased assembly
// We use 'LayoutLib::' to force the compiler to look in that specific DLL.
var btn = new LayoutLib::Grid.UI.Controls.Button();
// ✅ Correct: Accessing the Class from the global (default) assembly
// This refers to the class 'Controls' in 'Grid.Core.dll'
var ctrlClass = new Grid.UI.Controls();
}
}
Solution 2: Rename the Type or Namespace
If you have control over the source code for one or both of the libraries (i.e., they are your own projects), the best solution is to rename one of the artifacts to follow .NET Naming Guidelines.
The .NET Framework Design Guidelines recommend: Do not name a class the same name as a namespace.
Solution: rename the class Controls to ControlsManager or ControlList.
// In Grid.Core.dll
namespace Grid.UI
{
// ✅ Correct: Renamed to avoid conflict with the 'Grid.UI.Controls' namespace.
public class ControlsManager
{
public int Count;
}
}
Conclusion
CS0434 is a collision between two different assemblies fighting over the same name path.
- Understand the Cause: You have a
Namespace.Thingin DLL A and aClass ThinginsideNamespacein DLL B. - Preferred Fix (If possible): Rename the class or namespace to be distinct.
- External Fix (If read-only): Use
extern aliasin your project properties to give one of the DLLs a unique root name, and access it usingAlias::Namespace.