Tidbit Tuesday (on a Friday) – Extensions

For my first Tidbit I wanted to talk briefly about Extension Methods, which are very useful (especially when dealing with game engines like Unity). Extension Methods allow you to ‘add‘ new methods to existing classes, types and interfaces. This is useful if you do not have access to the original class or if you do not want to recompile that class. Extension methods are also commonly used by teams as an easy way to share utility methods. Personally I have a collection of extensions that I import into almost every project I work on just to make my life that much easier.

Using pre-existing extensions are easy, chances are you have worked with them before without even know it. The most common .Net extension is LINQ.  To use an existing extension you just need to add a reference to it’s parent class (i.e. using System.Linq;), then any type that is extended will automatically obtain the related methods.  If you are using an IDE that has an IntelliSense like feature it will kick in and make programming that much easier because those method will now show up after the ‘dot‘.

To create an extension method we simply add this before the first parameter (which is also the type we are extending); for example if you were constantly needing a random boolean value we could  extend the Random class:

Now instead of having to type if (myRandom.NextDouble() >= 0.5){…} we can type if (myRandom.Boolean()){…}.  This is just a simple, easy to understand example, to show you how it is done.

Extension methods are very versatile, they can extend variable types like int, float, string, etc…  You can also extend interfaces (as shown below) which is extremely useful since you cannot normally add methods (with working code) directly to an interface. By creating an extension to an interface every class that implements that interface now has access to that method.

The above is a bit more complicated (it uses the Fisher–Yates shuffle method if you were wondering) – and is much more useful, with this was can take a list or array and randomly shuffle its contents: var ShuffledList = OrginalList.Shuffle(); and the list is now reordered. Hopefully the example makes since – if you have any questions on how it works please ask in the comments.

Some other things to keep in mind when working with extensions.  To create an Extension Methods both the class and the methods must be static.  Extension Methods do not have access to the private or protected fields, and probably wont have access to internal fields either. Also (very important), you can’t override a method that already exists – the system won’t generate any warnings or errors – your method just won’t get called. Keep those things and mind and you should have no problem creating your own extensions.

Hopefully this gives you a basic understanding of extensions and their usefulness. Tune in next time for some more Tidbits (hopefully on a Tuesday) and feel free to let me know in the comments if you have any questions, noticed any mistakes or have ideas/requests for an upcoming article.

Posted in Beyond Mindless, Code Snippets, Programming and tagged , , , , , , .