Skip to main content

How to Resolve Error "CS0264: Partial declarations of type must have the same type parameter names in the same order" in C#

The Compiler Error CS0264 is a consistency error regarding Generic Partial Classes. The message reads: "Partial declarations of type 'MyClass<T>' must have the same type parameter names in the same order".

When using the partial keyword to split a class definition across multiple files, the compiler merges them into a single type during the build process. If that class is Generic (e.g., MyClass<T>), the compiler requires strict consistency across all files. You cannot name the generic placeholder T in one file and TValue in another, nor can you swap the order of multiple parameters. They must match exactly.

This guide explains the rules for defining generic partial classes correctly.

Understanding Partial Generics

To the compiler, partial class Data<T> implies that T is the standard name used throughout the entire scope of the class to refer to that specific generic argument.

If File A uses <T> and File B uses <U>, the compiler faces a dilemma:

  1. Are these the same parameter?
  2. If File A has constraints (where T : class), do they apply to U?

To avoid ambiguity, C# enforces a simple rule: Copy and paste the class signature exactly.

Scenario 1: Mismatched Parameter Names

This is the most common cause. A developer creates a second file for an existing class but gives the generic parameter a slightly different name (e.g., T vs TItem).

Example of error

File 1: Repository.cs

namespace MyApp
{
public partial class Repository<T>
{
public void Add(T item) { }
}
}

File 2: Repository.Extra.cs

namespace MyApp
{
// ⛔️ Error CS0264: Partial declarations must have the same type parameter names.
// 'TItem' does not match 'T' defined in the other file.
public partial class Repository<TItem>
{
public void Remove(TItem item) { }
}
}

Solution: Align the Names

Rename the type parameter in the second file to match the first.

Corrected File 2:

namespace MyApp
{
// ✅ Correct: Both files use '<T>'
public partial class Repository<T>
{
public void Remove(T item) { }
}
}

Scenario 2: Mismatched Parameter Order

If your class has multiple generic parameters, their order matters. <Key, Value> is a different signature than <Value, Key>.

Example of error

File 1: Lookup.cs

// Defined as Key, Value
public partial class Lookup<TKey, TValue>
{
}

File 2: Lookup.Methods.cs

// ⛔️ Error CS0264: Parameter names must be in the same order.
// Here we swapped them (Value, Key).
public partial class Lookup<TValue, TKey>
{
}

Solution: Match the Order

Ensure the sequence of parameters is identical in all partial declarations.

Corrected File 2:

// ✅ Correct: Matches <TKey, TValue>
public partial class Lookup<TKey, TValue>
{
}
note

Constraint Consistency: While CS0264 handles names, you might also encounter CS0265 if the constraints (e.g., where T : new()) are inconsistent. Usually, you only define constraints on one of the partial declarations to avoid redundancy, but if you define them on both, they must match perfectly.

Conclusion

CS0264 is a synchronization requirement for partial classes.

  1. Check Names: T must be T in every file. TValue cannot be TVal.
  2. Check Order: <A, B> must remain <A, B> everywhere.
  3. Best Practice: When adding a new partial file, copy the public partial class Name<...> line directly from the original file to ensure exact compatibility.