C#中协变逆变的实现
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>1. 协变与逆变的概念</li><li>2. 协变与逆变的作用及作用对象</li><li>3. 协变与逆变的关键字</li><li>4. 泛型接口与委托的示例</li><ul class="second_class_ul"><li>示例1:协变在泛型接口中的体现</li><li>示例2:逆变在泛型接口中的体现</li><li>示例3:协变在泛型委托中的体现</li><li>示例4:逆变在泛型委托中的体现</li></ul><li>总结</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>1. 协变与逆变的概念</h2><ul><li><p><strong>协变(Covariance)</strong><br />允许将子类(派生类)类型作为父类(基类)类型使用。例如:IEnumerable<string> 可以被视为 IEnumerable<object>,因为 string 是 object 的子类。按照前面我们在继承中学习的里氏替换原则,父类装子类,是不是就非常的合理,因为一定会确保类型安全嘛。</p></li><li><p><strong>逆变(Contravariance)</strong><br />允许将父类类型作为子类类型使用。例如:Action<object> 可以被视为 Action<string>,因为 object 是 string 的父类。你看,居然父类可以变成子类,你想想,你的父亲有一天居然可以化形为你的儿子来使用,是不是就很反常识,不过这种转化肯定是有限制滴,不然啥都可以转化,是不是就整个面向对象就乱成了一锅粥。</p></li></ul>
<blockquote><p>核心思想:在类型安全的前提下,允许更灵活的泛型类型转换。</p></blockquote>
<p class="maodian"></p><h2>2. 协变与逆变的作用及作用对象</h2>
<p><strong>作用</strong></p>
<ul><li><p>提<span> </span>高泛型接口和委托的灵活性,使其能更自然地处理继承关系。</p></li><li><p>减少强制<span> </span>类型转换,增强代码的可读性和安全性。</p></li></ul>
<p><span><strong>作用对象</strong></span></p>
<ul><li>泛型接口(如 IEnumerable<T>)</li><li>泛型委托(如 Func<T>、Action<T>)</li><li>不适用:类、结构体或方法参数(仅支持接口和委托)。</li></ul>
<p>请务必记住协变逆变的作用对象,仅仅只能在泛型接口和泛型委托中使用,在其他的地方是万万不行滴,不然就会天下大乱啦!!! </p>
<p class="maodian"></p><h2>3. 协变与逆变的关键字</h2>
<p><code>out</code><strong> 关键字(协变)</strong><br />标记泛型类型参数为协变,表示该参数仅用于<strong>输出位置</strong>(如返回值)。啥意思?就是说T这个泛型类型,在被out修饰了以后,他就只能被当成返回值的类型了,不能当做函数中传参时候的类型了,为什么呢?因为你总要做些区分嘛,要先列好规矩,互相才能正常运行嘛,不然,你玩儿你的,我打我打的,整个继承和多态就乱成了一锅粥。</p>
<div class="jb51code"><pre class="brush:csharp;">interface ICovariant<out T> { T GetItem(); }</pre></div>
<p><code>in</code><strong> 关键字(逆变)</strong><br />标记泛型类型参数为逆变,表示该参数仅用于<strong>输入位置</strong>(如方法参数)。啥意思?就是说T这个泛型类型,在被in修饰了以后,他就只能被当成函数传参时候的类型了,不能当做返回值类型。</p>
<div class="jb51code"><pre class="brush:csharp;">interface IContravariant<in T> { void Process(T item); }</pre></div>
<p class="maodian"></p><h2>4. 泛型接口与委托的示例</h2>
<p class="maodian"></p><h3>示例1:协变在泛型接口中的体现</h3>
<div class="jb51code"><pre class="brush:csharp;">// 协变接口定义
interface IAnimal<out T>
{
T GetAnimal();
}
class Animal { }
class Dog : Animal { }
class AnimalShelter : IAnimal<Animal>
{
public Animal GetAnimal() => new Animal();
}
class DogShelter : IAnimal<Dog>
{
public Dog GetAnimal() => new Dog();
}
// 使用协变
IAnimal<Animal> shelter = new DogShelter(); // 合法:DogShelter → AnimalShelter
Animal animal = shelter.GetAnimal(); // 安全获取Animal类型</pre></div>
<p class="maodian"></p><h3>示例2:逆变在泛型接口中的体现</h3>
<div class="jb51code"><pre class="brush:csharp;">// 逆变接口定义
interface IFeeder<in T>
{
void Feed(T animal);
}
class Animal { }
class Dog : Animal { }
class AnimalFeeder : IFeeder<Animal>
{
public void Feed(Animal animal) => Console.WriteLine("Feeding animal");
}
class DogFeeder : IFeeder<Dog>
{
public void Feed(Dog dog) => Console.WriteLine("Feeding dog");
}
// 使用逆变
IFeeder<Dog> feeder = new AnimalFeeder(); // 合法:AnimalFeeder → DogFeeder
feeder.Feed(new Dog()); // 安全传递Dog类型给需要Animal的方法
//打印Feeding animal</pre></div>
<p class="maodian"></p><h3>示例3:协变在泛型委托中的体现</h3>
<div class="jb51code"><pre class="brush:csharp;">// 协变委托
delegate T Factory<out T>();
Factory<Dog> dogFactory = () => new Dog();
Factory<Animal> animalFactory = dogFactory; // 合法:Dog → Animal
Animal animal = animalFactory();</pre></div>
<p class="maodian"></p><h3>示例4:逆变在泛型委托中的体现</h3>
<div class="jb51code"><pre class="brush:csharp;">// 逆变委托
delegate void Handler<in T>(T obj);
Handler<Animal> animalHandler = (Animal a) => Console.WriteLine("Handle animal");
Handler<Dog> dogHandler = animalHandler; // 合法:Animal → Dog
dogHandler(new Dog()); // 安全调用</pre></div>
<p class="maodian"></p><h2>总结</h2>
<table><thead><tr><th>特性</th><th>关键字</th><th>方向</th><th>适用场景</th></tr></thead><tbody><tr><td>协变</td><td>out</td><td>子类→父类</td><td>返回值、集合遍历(如 IEnumerable<T>)</td></tr><tr><td>逆变</td><td>in</td><td>父类→子类</td><td>方法参数、回调处理(如 Action<T>)</td></tr></tbody></table>
<blockquote><p>协变:out修饰的泛型替代符,只能作为返回值,不能作为参数<br />逆变:in修饰的泛型替代符,只能作为参数,不能作为返回值<br />协变:父类委托容器可以装 子类泛型委托容器 <br />逆变:子类委托容器可以装 父类泛型委托容器</p></blockquote>
<p><strong>注意事项</strong>:</p>
<ul><li>协变类型参数必须仅用于返回值位置。</li><li>逆变类型参数必须仅用于方法参数位置。</li><li>不支持类的协变/逆变,仅限接口和委托。</li></ul>
<p>到此这篇关于C#中协变逆变的实现的文章就介绍到这了,更多相关C# 协变逆变内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>图文详解C#中的协变与逆变</li><li>C#逆变与协变详解</li><li>c#协变和逆变实例分析</li><li>详解c# 协变和逆变</li><li>C#中的协变与逆变深入讲解</li><li>详析C#的协变和逆变</li><li>C#中的协变与逆变小结</li><li>C#中的协变与逆变方式</li><li>C#实现协变和逆变案例</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]