views:

42

answers:

2

Could someone explain the meaning of the following portion of code :

private event UserChangedHandler m_UserChanged;
public event UserChangedHandler UserChanged
{
  add
  {
      if (m_UserChanged != value)
      {
        m_UserChanged += value;
      }
  }
}

thanks

+3  A: 

add { } is a construct much like get { } for properties, except add works on events. You're defining custom functionality here when adding delegates to an event.

In this case, this method body prevents consecutive duplicates (i.e. you can't register the same method/handler twice in a row).

So in this example:

public void HandlerUserChanged(object o, UserChangedEventArgs args)
{
     // some code
}

public void HandlerUserChanged2(object o, UserChangedEventArgs args)
{
     // some code
}

Later:

UserChanged += HandleUserChanged;
UserChanged += HandleUserChanged;
UserChanged(this, new UserChangedEventArgs());

The method HandleUserChanged will only fire once, even though you registered it twice. A normal event (without the add { }) would invoke the function twice.

However:

UserChanged += HandleUserChanged;
UserChanged += HandleUserChanged2;
UserChanged += HandleUserChanged;
UserChanged(this, new UserChangedEventArgs());

Will allow HandleUserChanged to fire twice, because the last-registered handler isn't ever the one being added. The == operator on events works on the LAST handler. (Thankyou to Matthew for bringing that to attention)

Aren
Although, the code above will not stop you from adding the same method twice. If you add a method A and a method B to the UserChanged event you can still add method A again.
Matthew Manela
@Matthew Manela: You're right, It'll only prevent consecutive adds. Still early in the morning, thankyou for pointing that out.
Aren
A: 

It strikes me as odd that m_UserChanged is declared as an event rather than just a delegate instance (is that the right terminology...I get confused w/ delegates). Events are analogous to a simple Property model in that they essentially wrap underlying fields within a pair of transparent methods.

The way I understand it, .Net allows for the creation of implcit (anonymous?) event and properties by taking something like this :

public int Value { get; set;}
public event EventHandler ValueChanged;

and then creating the respective underlying placeholder objects to generate something more like this:

private int _Value;
public int Value { get { return _Value;} set { _Value = value;}}

private EventHandler _ValueChanged;
public event EventHandler ValueChange { add { _ValueChanged += value;} remove { _ValueChanged -= value;}}

The underlying object can be defined explicitly of course, but what the code sample above looks like is a bit of a conflation between explicit and implicit event declaration...it looks like the following is actually being done (behind the scenes, as it were):

private UserChangedHandler _m_UserChanged; 
private event UserChangedHandler m_UserChanged { add { _m_UserChanged += value;} remove { _m_UserChanged -= value;}}
public event UserChangedHandler UserChanged 
{ 
  add 
  { 
      if (m_UserChanged != value) 
      { 
        m_UserChanged += value; 
      } 
  } 
} 

It doesn't really matter in the grand scheme of things, I don't guess, but it looks like an oversight.

Steven