Learning C#: about event

L

Luke

class TestNoEventKeyword

{
public static void Test()
{
Student s1=new Student();
Student s2=new Student();

s1.RegisterOK +=new
Student.DelegateRegisterOkEvent(Student_RegisterOK);
s2.RegisterOK +=new
Student.DelegateRegisterOkEvent(Student_RegisterOK);
s1.Register();
s2.Register();

Console.ReadLine();
}

static void Student_RegisterOK()
{
Console.WriteLine("Hello");
}
}

class Student
{
public delegate void DelegateRegisterOkEvent
();
public event DelegateRegisterOkEvent
RegisterOK;
public string Name;

public void Register()
{
Console.WriteLine("Register Method");
RegisterOK();
}
}

When calling TestNoEventKeyword.Test(); in Main
(), it will output:

Register Method

Hello

Register Method

Hello



QUESTION: how if we remove the modifier event for
RegisterOK? The output is just the same.
As I understand, delegate is like defing a function
pointer via typedef in C++, and the so-called event
RegisterOK is a function pointer; Student_RegisterOK is
the underlying function attached to the pointer. What is
the difference if not use event for RegisterOK? (without
regard to event-accessors)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top