How to Resolve Error "CS0721: 'type': static types cannot be used as parameters" in C#
The Compiler Error CS0721 is a type system restriction error. The message reads: "'StaticClass': static types cannot be used as parameters".
In C#, a static class creates a container for global members that cannot be instantiated. You cannot write new StaticClass(), nor can you have a variable declared as StaticClass myVar. Since methods define their parameters as variables (e.g., void Method(Type variable)), and a static class cannot be a variable, it is syntactically impossible to define a method that accepts a static class as an argument.
This guide explains why static classes cannot be passed around and how to restructure your code to share data or logic.
Understanding Static Classes as Parameters
To pass an argument to a method, you need an Instance (an object in memory) or a Value (on the stack).
- Standard Class:
public class User {}. You can createnew User(). You can passUser uas a parameter. - Static Class:
public static class Config {}. You cannot create instances. There is no "object" to pass.
If you try to write void Configure(Config c), the compiler rejects it because it is impossible for the caller to ever provide a valid argument for c.
Scenario 1: Passing a Utility Class
This error often occurs when a developer creates a "Global Settings" static class and tries to pass it into a constructor or method for dependency injection purposes.
Example of error:
public static class AppSettings
{
public static string Theme = "Dark";
}
public class MainWindow
{
// ⛔️ Error CS0721: 'AppSettings': static types cannot be used as parameters.
// You cannot declare a variable (parameter) of type 'AppSettings'.
public void Initialize(AppSettings settings)
{
// ...
}
}
Scenario 2: Extension Methods on Static Classes
Extension methods allow you to add methods to existing types using the this keyword on the first parameter (e.g., this string s).
Developers sometimes try to add extension methods to a static class (e.g., adding a method to System.Console or System.Math). Since the first argument of an extension method defines the "target" type, and static classes cannot be parameters, you cannot write extension methods for static classes.
Example of error:
public static class MathExtensions
{
// ⛔️ Error CS0721: Cannot use static type 'System.Math' as a parameter.
// You cannot extend a static class.
public static void Log(this System.Math math, double value)
{
// ...
}
}
Solution 1: Remove the static Keyword
If you need to pass the class around, inject it into constructors, or treat it as an object with state, it should not be a static class.
Solution: make it a standard class (and optionally use the Singleton pattern if you need a single global instance).
// ✅ Correct: Removed 'static'. Now it defines an object.
public class AppSettings
{
public string Theme = "Dark";
}
public class MainWindow
{
// ✅ Correct: We can now pass an instance of AppSettings.
public void Initialize(AppSettings settings)
{
Console.WriteLine(settings.Theme);
}
}
Solution 2: Use Direct Access (No Parameter)
If the class is meant to be a global utility (like System.Math or a Constants class), you do not need to pass it as a parameter. Static members are globally accessible. Simply remove the parameter and access the class directly by name inside the method.
Solution:
public static class AppSettings
{
public static string Theme = "Dark";
}
public class MainWindow
{
// ✅ Correct: Removed the parameter.
public void Initialize()
{
// Access the static class directly via its name.
Console.WriteLine(AppSettings.Theme);
}
}
For Extension Methods:
If you wanted to extend System.Math, you cannot. You must simply write a new static utility method: MyHelper.Log(double val).
Conclusion
CS0721 prevents you from defining a method signature that can never be called.
- Check the Type: Is the parameter type defined as
public static class? - Determine Intent:
- Pass Data: If you need to pass data, remove
staticfrom the class definition so you can create objects. - Global Logic: If you just want to use the functions, remove the parameter from the method and call
ClassName.Method()directly inside the code.
- Pass Data: If you need to pass data, remove