- Delegates are type safe mechnism for call back functions.
- Call back can be static or Instance methods.
- Represent the address of a function
- Allow co-variance and contravariance (C# and .net allow these for reference types not for value type or Void)
- Covariance - return type can be derived from delegate's return type
- Contravariance - Mthd parameter can be the base of delegate parameters. for ex:
delegate object MyCallback(Filestream s);
string delegateInstance(stream s); - valid.
int delegateInstance(stream s); - not valid as int is value type
- Ultimately delegates are classes. So they can be defined anywhere a class can be defined. delegate:Multicastdelegate:system.Object.
- Compiler create a class for any defined delegate with four methods - constructor, Invoke, BeginInvoke and EndInvoke. Properties of multicast delegtes are _target (referes to object to operate on. In static method it is null) ,_methodPtr (mthd to callback) and _invocationList (refers to array when delegate chain is used else null).
- When chain of delegate is used and callback methods return some value, then finally return value of last callback method is returned. To control invocation and get all return values we have to manually loop delegate array and process it.
EVENTS:
- It's special Type which provide notification capabilities to a member defining this type.
- Based on delegates.
- By default event is based on delegate type which takes two parameters (object sender,EverntArgs e)
No comments:
Post a Comment