Skip to main content

Variance in C#.net 4.0 Covariance and Contravariance

Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangability or equivalence in certain situations (such as parameters, generics, and return types).
  • covariant: converting from narrower (float) to wider (double).
  • contravariant: converting from wider (double) to narrower (float).
  • invariant: Not able to convert (Null).

Concept Of Variance :

If y is derived from X and y relates to X then we can assign X to y. Like X=y. This is Covariance.
If Y is derived from X and y relates to X then we can assign y to X. Like y = X. This is Contravariance.
If class A is related to B then B is related A.
Covariance
Class B: A then we can assign A = B
Contravariance
Class B: A then we can assign B = A

Example

Class String:Object
Covariance => Object=string;
Contravariance=> string=Object;
Covariance interface and delegate must have 'In' keyword declaration for input parameter. Contravariance interface and delegate must have 'Out' keyword declaration for output parameter.

http://www.codeproject.com/KB/cs/CSharpVariance.aspx

Comments

Popular posts from this blog

ASP.NET 5 and MIDDLEWARE

ASP.NET 5 has been largely rewritten from the ground up, and incorporates some radical changes when compared with previous versions of ASP.NET. One of the biggest changes is in the HTTP Pipeline. This article looks at how those changes impact the design and registration of plug and play components that used to be represented by HttpModules. full blog 

MAUI to replace Xamarin

NET Multi-platform App UI (MAUI) NET Multi-platform App UI (MAUI) is a multi-platform technology for developing mobile (iOS and Android) and desktop (Windows and Mac) applications. NET MAUI is an evolution of Xamarin. Forms, they have most of their features in common. You can get almost all things in . NET MAUI that Xamarin has, like controls, layouts, Shell, gestures, templates, and cross-platform APIs for device features. Disadvantages It is a new product that has just been released and, although the released version is stable, some essential controls such as maps are still missing. Does Maui replace WPF? Building a WPF app is not supported in MAUI - as noted, they are two different UI implementations which both run on . Net 6. WPF is Windows specific, while MAUI is cross-platform. Compared to Flutter Flutter apps tend to be smaller and faster than those built with . NET MAUI. This is partly due to the small size of the compiled code. Overall, Flutter provides a more efficient and fl...

Whats new in Visual Studio 2015

Single sign-in You, like many other developers today, take advantage of multiple cloud services when developing your applications. For example, you've probably added a cloud backend to your applications to store data, stored your source code in the cloud, or published an application to a store. In the past, each of these services required a separate sign-in process, and each service managed the signed-in user state separately. With this release, reducing the authentication prompts required to access many integrated cloud services in Visual Studio. Now, when you authenticate to the first cloud service in Visual Studio, will automatically sign you in, or reduce the authentication prompts for other integrated cloud services. CodeLens In CTP 6, you can now see the history of your C++, SQL, or JavaScript files versioned in Git repositories by using CodeLens file-level indicators. Code Maps When you want to understand specific dependencies in your code, visualize them by cr...