Posts

Showing posts from November, 2013

.Net Contravariance & Covariance notes

Variant == Contravariant or Covariant. Invariant == NOT (Contravariant or Covariant) All next members of an interface will result in compilation errors. interface IVariant < out TReturn , in TArgument > {     // CS1961 Invalid variance:     // The type parameter 'TReturn' must be contravariantly valid on 'IVariant<TReturn, TArgument>.SetA(TReturn)'.     // 'TReturn' is covariant.     void SetA( TReturn smpleArg);     // CS1961 Invalid variance:     // The type parameter 'TArgument' must be covariantly valid on 'IVariant<TReturn, TArgument>.GetA()'.     // 'TArgument' is contravariant.     TArgument GetA();     // CS1961 Invalid variance:     // The type parameter 'TArgument' must be covariantly valid on 'IVariant<TReturn, TArgument>.GetASetR(TReturn)'.     // 'TA...

How to invoke an event handlers asynchronously

How to invoke an event or delegate handlers asynchronously Variant with the  Parallel class (TPL). public void OnMyEvent( String myValue) {     var multiCastDelegate = Interlocked .CompareExchange( ref MyEvent, null , null );     if (multiCastDelegate == null )      {         return ;      }     var handlers = multiCastDelegate.GetInvocationList()         .Cast< EventHandler < MyEventArgs >>();     var args = new MyEventArgs (myValue);     Parallel .ForEach(         handlers,         handler => handler( this , args)); }   Old variant with the Asynchronous delegates (APM). public void OnMyEventOld( String myValue) {     var multiCastDelegate = Interlocked .CompareExc...