Ok guys, quick c# lesson today!
Now one of the fundamental ideals of the c# language is that things similar in concept should be similar in syntax. This is why we use those cool getters are setters that are identical in syntax to normal fields. There is also a way to make this work for some methods too that makes a static method seem non-static, when it really should be non-static.
Let's take for example a random number generator.
static void Main(string[] args)
{
Random r = new Random();
r.Next();
r.NextBytes();
r.NextDouble();
}
Using the standard RNG that comes with the .net framework, we can get ints, bytes and doubles. Well that is cool, but what if we wanted to get a random bool? Pretty simple!
static void Main(string[] args)
{
Random r = new Random();
bool randomBool = r.Next(0, 1) == 1 ? false : true;
}
But this is something we might have to do a lot in something like level gen, so it would be annoying and a little silly to repeat this line of code over and over and over. Instead it would make a lot more sense to make a function for it!
static void Main(string[] args)
{
Random r = new Random();
bool randomBool = NextBool(r);
}
public static bool NextBool(Random r)
{
return r.Next(0, 1) == 1 ? false : true;
}
There we are. Nice, neat, atomic and generic. Everything we could ever need in a function, right? Well let's see how it looks next to those other functions.
static void Main(string[] args)
{
Random r = new Random();
r.Next();
r.NextBytes();
r.NextDouble();
NextBool(r);
}
Sort of stands out, doesn't it? It doesn't make sense for a function that is similar in concept to three others to have a different syntax. Lucky for us, c# offers a way to staple new methods onto already defined classes, using the 'this' keyword! Methods defined in this way are more limited than normal methods. For a start, these methods will not have access to a classes private attributes, this is to stop you from using it as a way to hack into the tasty interior of a class, rather than use it to keep your code neat and understandable. Secondly, these methods must be defined in a static class, to make sure that the method of the called class is not changed by the calling class through polymorphism shenanigans, because that would be conceptually stupid.
Now, let's update that code.
class Program
{
static void Main(string[] args)
{
Random r = new Random();
r.NextBool();
}
}
static class RandomExtendor
{
public static bool NextBool(this Random r)
{
return r.Next(0, 1) == 1 ? false : true;
}
}
As you can see I added 'this' before the first parameter. This tells the compiler that the first parameter will be used for calling the method, rather that inside the parameters. It can only be used once, and any other parameters you want to add will need to go inside the brackets as normal.
This not a hack, it compiles exactly the same as using a normal static method, it doesn't even make the method thread safe, you still need to do that yourself. It
does however make your code a lot more understandable. After all, it is damn annoying trying to remember where you put that method when it should be attached to the class.