Answered by:
Strange problem with events in C# 4.0

Question
-
I am using Visual Studio 2010 RC1
The following code works:
public event EventHandler<BackgroundProcessorEventArgs> ActionFailed; protected void OnActionFailed(Action<object> action, object parameter) { // Copy to a temporary variable to be thread-safe. EventHandler<BackgroundProcessorEventArgs> temp=null; lock (_locker) { temp = ActionFailed; _failed++; } if (temp != null) temp(this, new BackgroundProcessorEventArgs(action, parameter)); }
Now when I add an add and remove handler to the event I get a strange compiler error:
public event EventHandler<BackgroundProcessorEventArgs> ActionFailed { add { lock (_locker) { ActionFailed += value; } } remove { lock (_locker) { ActionFailed -= value; } } } protected void OnActionFailed(Action<object> action, object parameter) { // Copy to a temporary variable to be thread-safe. EventHandler<BackgroundProcessorEventArgs> temp=null; lock (_locker) { temp = ActionFailed; _failed++; } if (temp != null) temp(this, new BackgroundProcessorEventArgs(action, parameter)); }
The following error occurs in the line "temp=ActionFailed;" :
The event 'BackgroundProcessor.ActionFailed' can only appear on the left hand side of += or -=
I am not refering to += or -= in that line of code. Is this a bug or is this caused by the changes to events in C# 4.0?
Can someone explain the behaviour?
Best regards
Btw, posting to the forums does not work with Opera.
Saturday, March 20, 2010 11:11 AM
Answers
-
This has nothing to do with C# 4.0.
Declaring an event as
public event DelegateType EventName;
is equivalent to
DelegateType _EventName; public event DelegateType EventName { add { _EventName += value; } remove { _EventName -= value; } }
When your code uses EventName as the left operand of += or -=, the add or remove method is used. All other uses of EventName are compiled as uses of the Delegate field.
Now, in the second version, you declared the event as
public event DelegateType EventName { add { EventName += value; } remove { EventName -= value; } }
This has two effects. First, you're making the add and remove methods recursive. Second, since you explicitly declared the add and remove methods, there is no underlying Delegate field and then the compiler can only understand uses of the event as left operands of += or -=. All other uses are errors.- Proposed as answer by Tergiver Sunday, March 21, 2010 3:14 PM
- Marked as answer by Bin-ze Zhao Tuesday, March 23, 2010 8:45 AM
Saturday, March 20, 2010 8:52 PM
All replies
-
The implementation of events has changed in C# 4. Chris Burrows has just written a series of posts on this issue:
I don't know if they apply to your code but assuming it worked under C# 3, maybe they explain the new error message.
Saturday, March 20, 2010 12:05 PM -
This has nothing to do with C# 4.0.
Declaring an event as
public event DelegateType EventName;
is equivalent to
DelegateType _EventName; public event DelegateType EventName { add { _EventName += value; } remove { _EventName -= value; } }
When your code uses EventName as the left operand of += or -=, the add or remove method is used. All other uses of EventName are compiled as uses of the Delegate field.
Now, in the second version, you declared the event as
public event DelegateType EventName { add { EventName += value; } remove { EventName -= value; } }
This has two effects. First, you're making the add and remove methods recursive. Second, since you explicitly declared the add and remove methods, there is no underlying Delegate field and then the compiler can only understand uses of the event as left operands of += or -=. All other uses are errors.- Proposed as answer by Tergiver Sunday, March 21, 2010 3:14 PM
- Marked as answer by Bin-ze Zhao Tuesday, March 23, 2010 8:45 AM
Saturday, March 20, 2010 8:52 PM