0

I was looking at this question and while I think I mostly understand event accessors two other aspects of this confused me.

First:

private event Action<int> ActivityChanged = delegate {};

Was this event assigned a value with variable-initializers, my understanding was that only += and -= operators were permmited on events? What does the anonymous method do here?

Second:

event Action<int> IActivityFacade.ActivityChanged
{
    add
    {
        ActivityChanged += value;
        value(GetSelectedActivity()); 
    }
    remove { ActivityChanged -= value; }
}

Was the first line a forward declaration and the second a definition? I come from a C++/C background and that is what this appeared to me but for all I know it could mean something else entirely. Are forward declarations even allowed in C#

Colin Hicks
  • 348
  • 1
  • 3
  • 13
  • additionally, from the [microsoft docs](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-implement-custom-event-accessors) the variable name IDrawingObject.OnDraw has a member access in it? Is this also forward declaration? – Colin Hicks May 08 '20 at 04:10

1 Answers1

1
  • first line is private member and actual 'event' with dummy initializer. event have operators += and -= for subscribe and unsubscribe, without dummy it throws null reference
  • second is a publicly available accessor for subscribers with same += and -=

with member event you can fire them as follow this.ActivityChanged() some example

IActivityFacade.ActivityChanged this mean class implements interface and we explicitly define it as event accessor about interface events and explicit enterface

Radik
  • 2,715
  • 1
  • 19
  • 24