• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Dotnet Stuff

  • Home
  • Articles
  • Tutorials
  • Forums
  • Career Advice
  • Jobs
  • .NET FAQs
  • News
  • Privacy Policy

C# 2.0, Covariance And Contravariance In Delegates

In C# there is support for Covariance in arrays from C# version 1.0 itself.

With C# 2.0 release the support for covariance and contravariance in C# has extended to delegates also.

Delegates offer benefits such as passing methods as parameters to other methods, Single delegate for multiple functions, and callback functions.

The basic rule of thumb for a delegate is that the referenced method’s argument types and return type should match the delegate’s argument types and return types.

But this rule is a little relaxed with the covariance-contravariance support for delegates.

Covariance in C# basically means that the argument type of the method that is referenced by a delegate can have an argument type different from the delegate return type provided the argument type of the method is a subclass(derived) of the argument type of the delegate.

When you assign a method to a delegate that has a less derived return type, covariance allows you to do that.

You can use covariance to delegates, generic types, arrays, interfaces, and more.

Contravariance in C# means that the argument type of the method that is referenced by a delegate can have an argument type different from the delegate return type provided the argument type of the delegate is a subclass(derived) of the argument type of the method.

C# Contravariance is actually applied to parameters.

The ability to have a method parameter that is of a base class provided to a delegate that expects a method parameter of a derived class is known as contravariance.

See the code sample below for more clarity.

Here below two classes defined. One derived from the other

//base class
public class Shape
{
 
}
 
//A derived class
public class Rectangle : Shape
{
 
}

Table of Contents

    • 0.1  C# Covariance
    • 0.2  C# Contravariance
  • 1  Friends Assembly In C#

 C# Covariance

//Delegate with return type as base class
 public delegate Shape delgateCovariance();
 
//Method with return type as base class
 public Shape Method1()
 {
       return new Shape();
 }
 
 //Method with return type as derived class
 public Rectangle Method2()
 {
      return new Rectangle();
 }

Covariance Usage is :

delegateCovariance delgate1 = Method1;
delegateCOvariance delegate2 = Method2;

 C# Contravariance

//Delegate with return type as a base class

public delegate void delgateContravariance(Rectangle rect);

//Method with base type as parameter
public void Method1(Shape shape
{
}
 
//Method with derived type as parameter
public void Method2(Rectangle rectangle)
{

}
 Contravariance usage is :
delegateContravariance delgate1 = Method1;
delegateContravariance delegate2 = Method2;

 Friends Assembly In C#

Related Posts:

  • Differences Between Finalize and Dispose Methods in C# and VB.NET
    Differences Between Finalize and Dispose Methods in…
  • C# Version History | C# Evolution and Features
    C# Version History | C# Evolution and Features

Related posts:

  1. OWIN in ASP.NET Core| What is OWIN in .NET?
  2. C# Fixed Size Buffer
  3. Best Free .NET Decompilers and Assembly Browsers | C# decompiler free download
  4. Difference between Custom Controls,User controls and Components in C#.NET

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?
Log In
Increase Your Leadership Skills, Influence, and Power with Top Leadership courses starting at just $11.99.

Recent Posts

  • .NET Core Version History | Release History of .NET Core
  • What is string interpolation in C#
  • What Is The Use Of Volatile Keyword In C#
  • Get System IP Address Using C#
  • SQL Server Interview Question and Answers
  • C# Fixed Size Buffer
  • OWIN in ASP.NET Core| What is OWIN in .NET?
  • C# Version History | C# Evolution and Features
  • What is ASP.NET Core Module
  • C# 3d array

Recent Forum Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

Forums

  • .NET Framework
  • ASP.NET
  • C#
  • SQL

Recent Topics

  • What is the Difference Between IEnumerable and IQueryable in C#
  • How to remove Header of the XML file in C#?
  • What is DBMS?
  • What is RDBMS?
  • What is asp net framework ?

© Copywright 2017 Dotnetstuffs All Rights Reserved