in c#, games, programming, tutorial

Extension methods in C#

Share Button

Following the heritage of C++, C# comes with a number of powerful features which can either be used to massively improve your code …or to make it completely unreadable. In this post we’ll discuss a technique to add new methods to already existing classes. Yes, even classes you don’t have access to such as Vector3 Rigidbody and even string. Let’s introduce extension methods with a practical example.

The problem

Let’s imagine you have a game object with a rigid body you want to suddenly stop. You might want to do that for a number of reasons, such as pausing your game or stopping the time. A possible first approach is to set the relevant properties of the rigid body (velocity, angular momentum, etc…) to zero, an then set the property isKinematic to true. As a result your game object will not be controlled by PhysX any more. This is a possible way of doing it:

This method works, but is not very elegant.

Introducing extension methods

It would be much more elegant if the class Rigidbody2D itself had provided the Freezemethod. Luckily enough, C# allows you to add a method to a class from another file. This is done by creating a static class with has a static method, which has a Rigidbody2D as its first parameter. All the magic happens by adding the keyword this

By doing this, a new method called Freeze will be added to Rigidbody2D:

Both the class and the methods can have the name you like. If you are using Visual Studio or any other sufficiently advanced IDE, you’ll see that Freeze is marked as an extension method:

c

Handling nulls

It’s important to notice that extension methods are not methods in the “traditional” sense. The definition of the Rigidbody2D class has not been altered during compilation. Quite the opposite, the compiler changes any call to Rigidbody2D.Freeze to Rigidbody2DExtension.Freeze. This means that even if rigidbody2D is null, the function can still be invoked successfully. We can then embed all the integrity check directly into the Freeze method:

This is extremely powerful, because it allows to get rid of many null checks. At the same time, it can hide structural problems in your code and finding null references can be harder.

Saving the state

Let’s improve our code by saving the state of the rigid body so that it can be restored later:

which can be called as:

Adding references

If you’re going to invoke GetState a lot, creating new objects is not a good idea. The best way is to create the object once and then re-use it. We can then pass the instance to GetState so that it can be recycled.

Which can be invoked like this:

Other uses

C# has several interesting features which game developers rarely encounter. Learning to master a language so powerful can really speed up your coding. For instance, you can extend the class string to add this interesting method:

tmachineorg on Reddit suggested an extension method to get a component, or add it if it doesn’t exist:

Of, if you like the fancy double question mark operator:
Another interesting way of using extension method for Unity is to simply the way the position of an object is changed. Since transform.position is a property and not a variable, it cannot be changed directly; trying to write into into one of its fields (such as transform.position.x = 10) will result in an error. This can be solve by doing this:

Alternatively, you can “transform” Vector3s in packed arrays, like the one used in shaders:

Which allows to write:

Conclusion

Extension methods are powerful enough to make your code more expressive by creating safe shortcuts and aliases. But all these changes are not just cosmetic; if used properly they can dramatically reduce the number of mistakes and avoid code duplication. The only shortage of this technique is that it can’t be used to change an existing method. Creating an extension method with the same signature or another one is not an error; however, it will always be shadowed by the original one hence, never called.

Use extension methods responsibly.

Other resources

  • PicosRapture: A very beefy set of method extensions specifically designed for Unity;
  • PubSub from scratch: A more efficient way to implement a messaging system in Unity without SendMessage.
Support this blog! ♥

For the past three years I've been dedicating more and more of my time to the creation of quality tutorials, mainly about game development and machine learning. If you think these posts have either helped or inspired you, please consider supporting this blog.

Paypal
Twitter_logo

Don't miss the next tutorial!

There's a new post every Wednesday: leave your email to be notified!


Write a Comment

Comment

    • Hi! Extension methods are not that bad to be honest. Most of the job is done by the compiler. I’ve used them extensively, and didn’t notice any decrease in performance. It shouldn’t be any heavier than calling a static method.

Webmentions

  • Fading Sprites in Unity 5 - Alan Zucconi August 20, 2015

    Hmmm… compiler… yeah, syntax sugar, I bet that’s really what the story is. Awesome! Thanks!

  • Extension methods: probably the best feature of C# | Software Engineer InformationSoftware Engineer Information August 20, 2015

    Hmmm… compiler… yeah, syntax sugar, I bet that’s really what the story is. Awesome! Thanks!