1

What is the reasoning behind CA1009's (http://msdn.microsoft.com/en-us/library/ms182133.aspx) that 'sender' should always be 'object'?

For example, I have an class like Car that emits Moved events. I don't want Moved events being generated from any other classes. I imagine most events are specific to a particular class hierarchy so why doesn't the rule allow any exceptions?

user2864740
  • 60,010
  • 15
  • 145
  • 220
noctonura
  • 12,763
  • 10
  • 52
  • 85
  • How do you know it was a Car that moved? Could have been a Pedestrian. Could be an Airplane. You are not supposed to care, all that matters is that *something* moved. If you want to know what *specific* object moved then all you need is reference identity. Object is good enough for that. – Hans Passant Sep 26 '14 at 23:55
  • I suppose it's for historical reasons. Events date back to .NET 1.0, which didn't have generics yet. – Lucas Trzesniewski Sep 26 '14 at 23:56
  • 1
    Note this is a "recommendation", not a "requirement" - excepting as such recommendations are treated as requirements. – user2864740 Sep 26 '14 at 23:58
  • @HansPassant I'm writing the API and I only want this event sent by Cars. If I wanted the event to be sent by anything, i'd use object as the sender. – noctonura Sep 26 '14 at 23:58
  • 1
    How could you possibly get that wrong? It is up to the Car object to fire the event. If it is a Pedestrian then just don't fire it. Next year, you don't know that it might be something else. Decoupling the observer from the object is a Really Big Deal. – Hans Passant Sep 27 '14 at 00:00

0 Answers0