Skip to main content

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies.

Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them.

Enough philosophy, let’s get to the business.

Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further.

Invariance:

Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either at assignment or as passing values to the parameters.

For example if the expected value is an array of integers (int []), and we try to pass an array of doubles (double []), this will definitely result in error.

(Please Note: some types of higher order may be used in place of lower order while assigning values or passing parameters to a function)

Variant

A type must be a sub-type for another type to be a variant. Variant, as the name suggests is something that can vary. Let’s consider reference Types in .Net. All reference types ultimately derive from System.Object.

As we said in the beginning any subtype of a base type can act as variant.

How does a reference type behave as variant?

When a subtype is used in place of a base type (string in place of an object), an implicit conversion takes place and is legally acceptable.

Variant has two sub-categories

1. Covariance


2. Contra-variance

Covariance:

Covariant involves being able to use a more derived type as a substitute, e.g. a string in place of an object. Let’s consider an example to prove this.

string[] a = new string[1];

object[] b = a;

Response.Write(b[0]);



String is a sub-type of an object type and hence the code works fine with no error.

Covariance is applicable not only for assignments but also for return types from methods. It permits a method to have a more derived return type than what is defined in the delegate.

Contra-variance:

Contra-variance is just the opposite(rather juxtaposition image) of variance.In this, more specific can be used in place of derived types

Please visit the link to see a working example.


Following are the links that can be helpful in understanding the topic better.

Link1:

Link2:

Link3:


Hope this was Helpful.



Till next time we connect…Happy Coding.

Comments

Post a Comment

Popular posts from this blog

Asp.Net 4.0: An Overview-Part-III

This is the last post in the series which will explore the following new features of ASP.Net 4.0  Performance Monitoring for Individual Applications in a Single Worker Process Web.config File Refactoring Permanently Redirecting a Page Expanding the Range of Allowable URLs Performance Monitoring for Individual Applications in a Single Worker Process It is a common practice to host multiple ASP.NET applications in a single worker process, In order to increase the number of Web sites that can be hosted on a single server. This practice results in difficulties for server administrators to identify an individual application that is experiencing problems. ASP.NET 4 introduces new resource-monitoring functionality introduced by the CLR. To enable this functionality, following XML configuration snippet is added to the aspnet.config configuration file.(This file is located in the directory where the .NET Framework is installed ) <?xml version="1.0" encoding="UTF-8...

WCF-REST Services-Part-I

What is REST? REST stands for Representational State Transfer. REST as described in MSDN, “is an architectural style that can be used to build software in which clients (user agents) can make requests of services (endpoints)”. REST is one way to implement a client-server architectural style. A service that uses the architectural style of REST is generally referred to as a RESTful service or endpoint. RESTful endpoint Building Blocks 1. Resources(What resources would the service Serve/Offer) 2. URI(Identifiers used to represent the resources) 3. HTTP Verbs (What parts of the uniform interface (HTTP verbs) are each URI going to support, like Get/Post etc.) I’ll develop a hypothetical system that will make use of these blocks in the next article, let us consider the theoretical aspects of the REST in this post. Why REST? As explained above, REST internally implements a Client/Server model that can be easily achieved by using SOAP with ASMX or WCF. Just for the discussion sake ...