Using different delegates

Possible duplicate:
What is the difference between a delegate and events?

Possible duplicate:
Difference between events and delegates and related applications

(copied from this duplicate )

When I need to raise an event, I do it

public delegate void LogUserActivity(Guid orderGUID);
public event LogUserActivity ActivityLog;

even it works

public delegate void LogUserActivity(Guid orderGUID);
public LogUserActivity ActivityLog;

What is the difference between the two of them

+3
source share
5 answers

There are three things:

  • Delegate Type Declaration
  • Creating a public delegate type variable
  • Creating a delegate type public event

- - , .. / . , , " " / , . ; .

, .

EDIT. , "no-op":

public event LogUserActivity ActivityLog = delegate{};
+3

, . - - , , /:

public event LogUserActivity ActivityLog
{
    add { ... }
    remove { ... }
}

, , , , .

+1

, :

: obj.ActivityLog = null;//

: obj.ActivityLog = null;//valid

, , / . , .

, , :

obj.RegisterActivityCallback(...)

+1

ActivityLog, :

logger.ActivityLog += new LogUserActivity(Listener1);
logger.ActivityLog += new LogUserActivity(Listener2);

(Listener1 Listener2) . , . :

logger.ActivityLog = new LogUserActivity(Listener1);

, . - , .

0

, IS . - , "" , , "" , , ( s), , ( ).

An event is a delegate with a certain signature, in particular, one that returns nothing (returns void) and takes two parameters - a System.object object to store a reference to any object that triggers the event, and an instance of any type obtained from System.EventArgs, to store any other data that the event should transfer from it from the initialtor to the handler.

0
source

Source: https://habr.com/ru/post/1716594/


All Articles