I hope many of you have come across the two buzz words "Co-Variance" and "Contra-Variance" while browsing through .NET portals. In this article, I will try to give an introduction to the concepts behind these buzz words.
Covariance and Contravariance are polymorphism extension to the arrays, delegates and generics. It provides implicit reference conversion for Arrays, Delegates and Generic parameter types. Covariance preserves the assignment compatibility and Contravariance opposite the covariance functionality.
Assignment Compatibility
Actually assignment compatibility is not new to the C# programmer. It is only the word "Assignment Compatibility" that is new.
Have a look at the following sample code:
String stringObject = "A String Object";
Object anObject = stringObject;
An Object of a derived class (stringObject) is being assigned to a variable of a base class (anObject).
Array Covariance
The Array data type has supported Covariance since .NET 1.0. While working with arrays, you can successfully make the following assignment.
object[] objArray = new String[10];
The code shown above assigns a string array to an object array variable. That is, a more derived class object's array (String []) is being assigned to the less derived class object array variable (object []). This is called covariance in array.
Array Covariance is not safe. Consider the following statement
objArray[0] = 5;
That statement will not report any compile time errors. But at runtime, it causes an ArrayTypeMismatchException exception. It is due to the fact that the objArray variable actually holds a reference of a string Array.
Delegate Covariance
This type of variance is also called method group variance. It allows Delegate instances to return more derived class types than what is specified in the type declaration.
In the following example of covariance, a string returning function is being assigned to a delegate which is declared to return object type.
static string GetString() { return ""; }
static void Main()
{
Func<object> delegateObject = GetString;
//String strObject = delegateObject();
}
Contravariant Delegate
The Contravariance Delegates reverse the Covariance functionality. It allows a method that has parameter types less derived than what is specified in the delegate.
In the following example a delegate specifies a parameter type as string. Still we can assign a method that has object as parameter.
static void SetObject(object objectParameter) { }
static void SetString(string stringParameter) { }
static void Main()
{
Action<string> del2 = SetObject;
.....
}
What is new in .NET 4.0 about Delegate Variant?

Shakti SaxenaPosted Aug 28, 2015, 2:08 AM
Thank you all
Gowtham RajamanickamPosted Aug 27, 2015, 9:51 AM
good
Rajeesh MenothPosted Aug 27, 2015, 3:29 AM
Nice One
Ankit BansalPosted Aug 27, 2015, 2:22 AM
Good one..Thanks for sharing..
Mohammed IbrahimPosted Aug 27, 2015, 12:09 AM
nice
Santhakumar MunuswamyPosted Aug 26, 2015, 11:21 PM
Good article. Thanks for sharing
Gowtham KPosted Aug 26, 2015, 11:04 AM
Good one
Yashwanth MuthineniPosted Aug 26, 2015, 6:10 AM
Nice Share
Vaikesh K PPosted Aug 26, 2015, 5:31 AM
Good One
Karthikeyan KPosted Aug 26, 2015, 5:25 AM
Good one sir..Thanks for share