Skip to main content

How to Resolve Error "CS0695: 'type' cannot implement both 'interface1' and 'interface2' because they may unify for some type parameter substitutions" in C#

The Compiler Error CS0695 is a Generic Type Unification error. The message reads: "'MyClass<T>' cannot implement both 'IInterface<T>' and 'IInterface<int>' because they may unify for some type parameter substitutions".

In C#, a class cannot implement the same interface twice with the same type parameters. For example, implementing IComparable<int> and IComparable<string> is fine (they are distinct). But implementing IComparable<T> and IComparable<int> is forbidden because if the user supplies int for T, the class would effectively try to implement IComparable<int> twice, creating a conflict the runtime cannot resolve.

This guide explains why this ambiguity occurs and how to restructure your generic classes to avoid it.

Understanding Interface Unification

When you define a generic class MyClass<T>, the compiler must ensure that the class definition is valid for all possible values of T.

Consider:

class MyClass<T> : IEnumerable<T>, IEnumerable<int> { ... }
  • If T is string: The class implements IEnumerable<string> and IEnumerable<int>. (Valid).
  • If T is int: The class implements IEnumerable<int> and IEnumerable<int>. (Invalid).

Because there is a possibility ("they may unify") that T creates a duplicate interface implementation, the C# compiler blocks the definition entirely to prevent runtime ambiguity.

Scenario: The Potential Conflict

This error commonly happens when you try to provide a "specialized" implementation for a specific type alongside a generic implementation.

Example of error: you want your container to handle generic types, but you also want it to specifically handle integers.

interface IProcess<T>
{
void Execute(T input);
}

// ⛔️ Error CS0695: 'Container<T>' cannot implement both 'IProcess<T>' and 'IProcess<int>'.
// If T becomes 'int', we have two 'IProcess<int>' contracts.
public class Container<T> : IProcess<T>, IProcess<int>
{
public void Execute(T input) { /* Generic logic */ }
public void Execute(int input) { /* Int logic */ }
}

The compiler sees that Container<int> would look like this: public class Container<int> : IProcess<int>, IProcess<int> (Impossible).

Solution 1: Remove One Interface Implementation

Usually, the generic interface covers the specific case anyway. If T is int, IProcess<T> is IProcess<int>. You don't need to list it twice.

You can handle the specific logic inside the generic method using type checking.

Solution:

public class Container<T> : IProcess<T>
{
public void Execute(T input)
{
// ✅ Correct: Check at runtime if T is int
if (input is int i)
{
Console.WriteLine($"Special integer logic: {i}");
}
else
{
Console.WriteLine($"Generic logic: {input}");
}
}
}

Solution 2: Create Distinct Base Classes

If you absolutely need distinct implementations (perhaps the logic is vastly different and you want to avoid if (typeof(T) == typeof(int))), you can split the inheritance hierarchy.

Create a base class for the specific case and derive the generic case from it, or define specialized classes that do not use generics for the conflict.

Specific Class for Int

// 1. Generic version
public class GenericContainer<T> : IProcess<T>
{
public void Execute(T input) { ... }
}

// 2. Specific version (Non-generic)
// ✅ Correct: This class explicitly handles int. It is not generic,
// so no substitution can cause a conflict.
public class IntContainer : IProcess<int>
{
public void Execute(int input) { ... }
}

Conclusion

CS0695 is a "What if?" error. The compiler protects you from a future ambiguity.

  1. Analyze the Interfaces: Are you implementing Interface<T> and Interface<SpecificType>?
  2. Ask: What happens if T equals SpecificType?
  3. The Fix: Remove the specific interface. The generic interface Interface<T> will naturally become Interface<SpecificType> when instantiated with that type. Handle special logic inside the method implementation.