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 

dotNET7

Performance is a key focus of .NET 7, and all of its features are designed with performance in mind. In addition, .NET 7 includes the following enhancements aimed purely at performance: On-stack replacement (OSR) is a complement to tiered compilation. It allows the runtime to change the code executed by a currently running method in the middle of its execution (that is, while it's "on stack"). Long-running methods can switch to more optimized versions mid-execution. Profile-guided optimization (PGO) now works with OSR and is easier to enable (by adding <TieredPGO>true</TieredPGO> to your project file). PGO can also instrument and optimize additional things, such as delegates. Improved code generation for Arm64. Native AOT produces a standalone executable in the target platform's file format with no external dependencies. It's entirely native, with no IL or JIT , and provides fast startup time and a small, self-contained deployment. In .NET 7, Native...

dotNET6

FileStream The System.IO.FileStream type has been rewritten for .NET 6 to provide better performance and reliability on Windows. Now, FileStream never blocks when created for asynchronous I/O on Windows. Profile-guided optimization Profile-guided optimization (PGO) is where the JIT compiler generates optimized code in terms of the types and code paths that are most frequently used. .NET 6 introduces dynamic PGO. Dynamic PGO works hand-in-hand with tiered compilation to further optimize code based on additional instrumentation that's put in place during tier 0. Dynamic PGO is disabled by default, but you can enable it with the DOTNET_TieredPGO environment variable. Crossgen2 .NET 6 introduces Crossgen2, the successor to Crossgen, which has been removed. Crossgen and Crossgen2 are tools that provide ahead-of-time (AOT) compilation to improve the startup time of an app. Crossgen2 is written in C# instead of C++, and can perform analysis and optimization that weren't possible with ...