About delegate and event.

G

Guest

Hello, everyone.
As we know, some event definded like following:

public delegate MyEventDelegate(object pram);
public class ClassA
{
public event MyEventDelegate MyEvent;
}

My question is must I check the instance of "MyEvent" is exist before raise
the event. For example:
if (this.MyEvent != null) this.MyEvent(...);
I don't think same situation in VB.NET.
 
D

Daniel O'Connell [C# MVP]

Steven.Xu said:
Hello, everyone.
As we know, some event definded like following:

public delegate MyEventDelegate(object pram);
public class ClassA
{
public event MyEventDelegate MyEvent;
}

My question is must I check the instance of "MyEvent" is exist before
raise
the event. For example:
Because the C# compiler basically treats accessing the event from within the
defining class as accessing the underlying delegate field.
I don't think same situation in VB.NET.

I don't think VB uses the same syntax to raise an event as it does to call a
method, thus it is free to allow event invocation code to do a null check
where the equivilent C# code would be surprising.

C# could solve this by adding support for raise and generating a safe null
check for you in trivial events.
 

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

Similar Threads


Top