How to Resolve Error "CS0305: Using the generic type 'type' requires 'number' type arguments" in C#
The Compiler Error CS0305 is a Generics Arity error. The message reads: "Using the generic type 'TypeName' requires 'X' type arguments".
In C#, Generics allow you to define classes, interfaces, and methods with placeholders for data types (Type Parameters). These are denoted by angle brackets, e.g., <T> or <TKey, TValue>. When you use a generic type, you must provide the exact number of concrete types that the definition expects. If the class expects two types (like a Key and a Value) and you provide only one (or none), the compiler raises CS0305.
This guide explains how to match your usage of generics with their definitions.
Understanding Type Arguments
When a class is defined as public class Box<T>, it has an "Arity" of 1. It requires one type argument.
When a class is defined as public class Map<TKey, TValue>, it has an "Arity" of 2. It requires two type arguments.
The compiler strictly enforces this count. You cannot instantiate Map without telling the compiler what TKey and TValue are.
Scenario 1: Missing Type Arguments entirely
This is common when developers accustomed to older versions of C# (or Java raw types) forget to specify the type for a generic collection like List<T>.
Example of error
Attempting to use List without the angle brackets <>.
using System.Collections.Generic;
public class Program
{
static void Main()
{
// ⛔️ Error CS0305: Using the generic type 'List<T>' requires 1 type arguments.
// The compiler found 'System.Collections.Generic.List<T>',
// but you treated it like a non-generic class.
List names = new List();
}
}
Solution: Specify the Type
Add angle brackets containing the type of data the list will hold.
using System.Collections.Generic;
public class Program
{
static void Main()
{
// ✅ Correct: We specify that this list holds 'strings'.
List<string> names = new List<string>();
}
}
If you actually intended to use the old, non-generic ArrayList, you must use System.Collections.ArrayList instead of List.
Scenario 2: Incorrect Number of Arguments
Some generic types require multiple arguments. Dictionary<TKey, TValue> is the classic example; it requires a type for the key and a type for the value.
Example of error
Providing only one type when two are required.
using System.Collections.Generic;
public class Cache
{
// ⛔️ Error CS0305: Using the generic type 'Dictionary<TKey, TValue>'
// requires 2 type arguments.
// We only provided 'string'.
public Dictionary<string> DataCache { get; set; }
}
Solution: Fill All Placeholders
Check the definition of the class (hover over it in your IDE) to see how many types it needs.
using System.Collections.Generic;
public class Cache
{
// ✅ Correct: Key is string, Value is object.
public Dictionary<string, object> DataCache { get; set; }
}
Scenario 3: Naming Conflicts (Non-Generic vs Generic)
It is legal in C# to have a non-generic class Data and a generic class Data<T> in the same namespace. However, if you only define Data<T>, but try to use it as Data, you get CS0305.
Example of error
You defined a generic class but tried to use it as a standard class.
// Definition
public class Repository<T>
{
public void Add(T item) { }
}
public class Program
{
static void Main()
{
// ⛔️ Error CS0305: 'Repository<T>' requires 1 type argument.
// You cannot use 'Repository' without the <T>.
Repository repo = new Repository();
}
}
Solution
Decide if you meant to use the generic version or if you forgot to define a non-generic version.
public class Program
{
static void Main()
{
// ✅ Correct: Using the generic version with 'User'
Repository<User> repo = new Repository<User>();
}
}
public class User { }
Arity Overloading: If you define public class Repository { } (non-generic) alongside Repository<T>, the error will disappear because new Repository() will resolve to the non-generic class.
Conclusion
CS0305 is a simple counting error.
- Count the Placeholders: Look at the class definition. Does it have
<T>,<T1, T2>, or<T1, T2, T3>? - Match the Usage: Ensure your code provides exactly that many types inside the angle brackets
<...>. - Check for Missing
<>: If you provided no brackets at all, you likely forgot that the class is generic.