Unity Error: Extension Method Must Be Defined in a Non-Generic Static Class
I’ve successfully kept my nose out of extension methods for a few years. As it turns out, they’re actually pretty helpful. I was working with extension methods the other day when Unity suddenly reported an error: “Extension Method Must Be Defined in a Non-Generic Static Class”. Oops! I did some research to understand the problem, and then I put my findings together into a blog post for you.
Long story short:
C# requires you to define an extension method in a non-generic static class. To fix the error, move the extension method to a suitable class. Or, change your class to fit the requirements.
For the curious among us, I’ve written up some more detail on this. What causes the error, details on how to fix it, practical examples, and further reading.
What you’ll learn
An Extension Method is a tool in C#. This tool enables you to extend the capabilities of an existing class. But, you may encounter an error: extension method must be defined in a non-generic static class. In this article, we will inspect this error and walk you through how to fix it.
Extension Method Basics
Extension methods make it possible to extend the capabilities of a class. Instances of that class call these methods as if they were instance methods of that type.
What you need to know
- C# requires you to define extension methods in non-generic static classes.
- To fix the error, move the extension method to a non-generic static class.
- Alternatively, modify the current class to make it non-generic and static.
How to fix the error
- Identify the extension method causing the error.
- Create a new non-generic static class to house the extension method.
- Transfer the method definition to the created class.
- Rebuild the project to confirm that you resolved the error.
Practical Example on Using Extension Methods
Imagine you’re developing a Unity game. The game has various features. Let’s just imagine that you need to extend the basic methods available to integer types. But, you’ve encountered this error in your IDE.
What do you do?
Assume that you have a component in your project. Assume you want to call an extension method on integer type.
using UnityEngine;
public class MyExtensionMethodDemo : MonoBehaviour
{
private void Start()
{
int myNumber = 42;
myNumber.MyExtensionMethod();
}
}
And so far, this is what you have for your Extension class and Extension method:
using UnityEngine;
// If you use a generic non-static class for your Extension method,
// the code will throw an error
public class MyExtensions<T>
{
// Define the extension method
public static void MyExtensionMethod<T>(this T obj)
{
Debug.Log("Extension method called on " + obj);
}
}
This code has two problems.
- You are using a generic class. You know this because you have an open <T> constructor).
- You are also using a non-static class. You know this because your class is missing the static keyword.
To fix this, we need to make your class non-generic and static. To do so, you need to remove the <T> constructor. You also need to add the static keyword.
Here’s what the correct code looks like
using UnityEngine;
// If you use a non-generic static class for your Extension method,
// the code will work
public static class MyExtensions
{
// Define the extension method
public static void MyExtensionMethod<T>(this T obj)
{
Debug.Log("Extension method called on " + obj);
}
}
If you save and run your project, you’ll see that the IDE warning is gone and that your project is working now. yeehaw cowboy.
Application Across C# and Unity
This rule is not unique to Unity. It applies in both classic C# programming as well as Unity. You must adhere to this rule in both.
Be a better game developer
This rule is a constant factor in both C# and Unity. Knowing about this rule now can help you in the future. If you are building traditional applications, knowing this will help you. If you are building for Unity, knowing this will help you. Be a pro, remember the rule.
Further reading
Not much else to say on this topic, but I compiled some resources to learn more about the error and the subject matter.
- Some more Extension method mishaps on StackOverflow: https://stackoverflow.com/questions/6096299/extension-methods-must-be-defined-in-a-non-generic-static-class
- Community Q&A for Unity: https://forum.unity.com/threads/error-extension-method-must-be-defined-in-a-non-generic-static-class.804339/
- Compiler error documentation: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1106
For more general information on extension methods:
- Guide from Microsoft: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
- Unity’s Extension Methods Tutorial: https://learn.unity.com/tutorial/extension-methods
Are you a Unity developer?
I’ll bet. Well, I make graphics packages and other tools for Unity. These tools help you make your game. They help you make it more quickly, and they help it look better when you’re done.
I know that building a game is truly a ton of work. That’s why I share free tools with the community to help beginning game developers make their games. And, I create powerful and easy-to-use assets for Unity for intermediate and advanced game developers, too.
Learn more about what I’ve made and how it can help your project on my assets page: https://occasoftware.com/assets