Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Delegate and MulticastDelegate classes are not considered as base class?

When I inherit Delegate or MulticastDelegate classes I am getting compiler error, both are normal abstract classes only even though why I am unable to inherit these classes.

Could you please tell me any one why these classes are not considered as base class?

like image 840
user3696930 Avatar asked Jun 01 '14 14:06

user3696930


1 Answers

Because it is very, very important that objects of a derived class of MulticastDelegate are immutable. Which ensures that state cannot get corrupted when arbitrary code you don't know about subscribes an event handler. Immutability in .NET is implemented by convention, you make fields private, don't provide property setters, don't provide a default constructor.

This is not a trait that can be inherited, you could derive a class for an immutable base class and make it mutable. And violate the immutability requirement.

So the compiler has a hard-baked rule about it and rejects your attempt at deriving from MulticastDelegate yourself. It reserves the right, insisting it can only do it properly itself, you have to use the delegate keyword. And does more, it automatically creates proper declarations for the Invoke() and BeginInvoke() methods, declarations that force you to use these methods in a type-safe way.

This will not slow you down.

like image 186
Hans Passant Avatar answered Oct 02 '22 00:10

Hans Passant