Delegate: I understand. But when I get to the event, I don’t understand many things. I read a book, MSDN and some simple examples on the Web, they both have the same structure. For example, here is the link: Event Example
I take the first example that the author said that this is the simplest example about a C # event.
Here is his code:
public class Metronome
{
public event TickHandler Tick;
public EventArgs e = null;
public delegate void TickHandler(Metronome m, EventArgs e);
public void Start()
{
while (true)
{
System.Threading.Thread.Sleep(3000);
if (Tick != null)
{
Tick(this, e);
}
}
}
}
public class Listener
{
public void Subscribe(Metronome m)
{
m.Tick += new Metronome.TickHandler(HeardIt);
}
private void HeardIt(Metronome m, EventArgs e)
{
System.Console.WriteLine("HEARD IT");
}
}
class Test
{
static void Main()
{
Metronome m = new Metronome();
Listener l = new Listener();
l.Subscribe(m);
m.Start();
}
}
You can see the line: public event TickHandler Tick. When I switch to public TickHandler Tick, the program still works the same. But I understand the new line, because it is just a pure delegate.
So, my question is: what is the real purpose of the keyword eventin the string: public event TickHandler Tick. This is very important because all examples always use this, but I cannot explain why.
Thank:)