Well, we almost dropped off page one... Can't have that! Time for a lesson, or depending on how I feel afterwards, two.
First up, will the C# students come to the front of the class, and grab one of these freshly cut cigars, then take a deep puff and blow smoke in the face of the Java student to your left. Today we are going to do something Java can't do! Well it has a work around, but we will do that another day.
Now we can pass a lot of things around as types; primitives, enums,
structs black magic, objects... But we can
also pass around methods! We do this using a thing called a Delegate. They have a return type, a name and parameters, observe.
class Program
{
static void Main(string[] args)
{
}
}
delegate void OnUpdate(int ticks);
So as you can see, we start with 'delegate', this declares a new delegate in the same way you use the 'class' keyword to make a new class. Next we have the return type, in this case void, but you can use anything. Then the name of the delegate: While classes are ideally named after singular common nouns, and methods are named after verbs, delegates should be named after events. For example, OnUpdate, KeyPressedDown, OnDeath, OnSunrise ect... Just like a lot of methods start with 'get' or 'set', most delegated end up starting with 'on'. Finally, we have the parameters, once again, anything you need.
Now, the return type + the parameters determine the signature of the delegate. Only methods with the same signature can fit into a delegate, just as if you have a car class, and a person class, you can't fit a person into a car variable.
Now just like when you make a class you still need to use it somewhere, the same is said for delegates! So let's add one to our short little program.
class Program
{
static void Main(string[] args)
{
OnUpdate myDelegate = new OnUpdate();
}
}
There! Just like making a new object! But wait, red lines, things are bad! We can't just make a new delegate without feeding it a method to run. It needs to know what you want it to do. Well we don't have any yet, so let's make a quick little method for it to run.
static void PrintTicks(int ticks)
{
Console.WriteLine("Simulation has been running for {0} ticks.", ticks);
}
Add that to your program class. Now over in your main, change that line to this.
static void Main(string[] args)
{
OnUpdate myDelegate = new OnUpdate(PrintTicks);
}
Notice that we
didn't add any parentheses after the name of the method? That is because we don't want it to run, we just want to pass it in. You use parentheses after a method when you want to invoke it. Now let's flesh out the main a little more and try it out!
static void Main(string[] args)
{
OnUpdate myDelegate = new OnUpdate(PrintTicks);
for (int i = 0; i < 100; i++)
{
myDelegate(i);
}
Console.ReadKey();
}
Run that, and check it out! You are running a method without having to reference to that method! Cool as! But sad to say I'm sort of running low on time here, and there is more to show off. I'll come back some time to finish up, but just play around with that, find out what you can and can not do. Protip: A lot more than you can do than you can't.