
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Covariance and Contravariance in C#
To deal with classes effectively, use the concept of covariance and contra variance.
Let us consider the following as our class. One is a base class for class Two, whereas Two is a base class for Three.
class One { } class Two: One { } class Three : Two { }
A base class can hold a derived class, but the opposite is not possible. With Covariance, you can pass a derived type where a base type is expected. Co-variance can be used on array, interface, delegates, etc in C#.
Contra variance is for parameters. A method with the parameter of a base class is allowed to be assigned to a delegate that expects the parameter of a derived class with Contravariance.
- Related Articles
- Difference between Covariance and Correlation
- How is Covariance and Correlation used in Portfolio Theory?
- What is the relationship between correlation and covariance?
- Program to find covariance in C++
- Write a program in Python to compute grouped data covariance and calculate grouped data covariance between two columns in a given dataframe
- How to create a covariance matrix in R?
- How to find the covariance between two vectors in R?
- How to find the covariance using the Series.cov() Method in Pandas?
- Bitwise AND and OR in Arduino
- Logical AND and OR in Arduino
- What is different in & and AND operators in Python?
- Using combination of “AND” and “OR” in SELECT in ABAP
- What is different in OR and AND operators in Python?
- Comparison between "&&" and "AND" operator in PHP.
- Isupper() and Islower() and their application in C++

Advertisements