Home > Backend Development > C#.Net Tutorial > Covariance and contravariance in C#

Covariance and contravariance in C#

WBOY
Release: 2023-09-07 09:05:02
forward
938 people have browsed it

C# 中的协变和逆变

To deal with classes efficiently, use the concepts of covariance and inverse variance.

Let's consider the following as our course. One is the base class for class two, and two is the base class for class three.

class One { 
}
class Two: One {
}
class Three : Two { 
}
Copy after login

Base classes can contain derived classes, but the reverse is not possible. Covariance allows you to pass a derived type where a base type is expected. Covariance can be used with arrays, interfaces, delegates, etc. in C#.

The contrast variance is for parameters. Allows methods with base class parameters to be assigned to delegates that require derived class parameters with contravariance.

The above is the detailed content of Covariance and contravariance in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template