C# EXTENSION METHODS WILL BECOME YOUR NEW BEST FRIEND

If you do not yet know about extension methods in C#, you’re in for a treat. If you do, well… maybe you will go home with some new inspiration for more handy extension methods.

For those who don’t know, with extension methods you can add new methods to existing types/classes, without having to make a new class that inherits from it. This is useful for adding handy little utility or helper methods for frequently used operations or calculations with types that you are not necessarily in control of, without having to create a new class that extends it. One example is some of the types that the Unity engine provides: Vector3 , Rect , Color , etc.. Since I mostly code in Unity, the following examples will be somewhat related to game development, although extension methods are a general feature of C# and definitely not exclusive to Unity

ALRIGHT. GIVE ME AN EXAMPLE.

When developing games, it is often that we need a directional vector from eg. the player to an enemy. As we all know, a vector from A to B is calculated as B – A. This looks like the following:

Now, you might have a properly functioning brain, but I sometimes struggle with remembering if a directional vector from A to B is B – A or A – B, so I wanted to make it easy for myself in the future (rather than actually memorizing it) by extending the Vector3 class with a method that automatically takes care of it for me.

LET’S GET OUR HANDS DIRTY

The first thing we need to do is create a class that will hold our extension methods. I’ve called mine Util:

Now, let’s add that method we talked about:

Note that although the method is static  it will be applied on a per-instance basis, thanks to the this  keyword in front of the first Vector3 parameter in the function definition. This results in the ability to call the method from any instance of a Vector3 , for instance the transform.position  of a script on the player object!

So now when I need to get the direction from the player to an enemy in my player script, i can simply type:

Thanks to this handy little method, I will never have to worry about which vector to subtract from which again! But I will still only have a 50% chance of answering correctly if anyone asks me 😕

MORE EXAMPLES

I’ve done a lot of work on editor extensions for the Unity editor, and one thing I often want to do is highlight something by drawing a semi-transparent white rect on top of it. However, there’s no easy way to quickly write “I want the color white, but with an alpha value of 0.5!” in the code! Again, extension methods to the rescue:

I’ll admit that at first it doesn’t look like it’s that much faster and/or more easy. However, imagine having some color property that you, or even the player, has picked. If this color is not exactly white or black, it becomes harder to read – not to mention that drawing that defined color, but with decreased alpha, becomes a mess!

Take a look:

CONCLUSION

So there you have it! Extension methods are extremely useful, and can end up saving you a lot of time, with the added bonus of making your code look much better!

I’ve only shown a couple of examples with vectors and colors so far, but keep in mind – extension methods can be added to any type you want! Get creative with it, and if you don’t mind, share your favorite extension methods with other people in the comments!

I’ll leave you with a few more examples to get the inspiration flowing!