栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

C#中的协方差有哪些?(或者,协方差:例如)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#中的协方差有哪些?(或者,协方差:例如)

这是我能想到的:

更新资料

阅读了埃里克·利珀特(Eric Lippert)提出的建设性评论和大量文章后,我改善了答案:

  • 更新了数组协方差的残缺性
  • 添加了“纯”委托方差异
  • 从BCL添加了更多示例
  • 添加了指向文章的链接,这些文章深入解释了这些概念。
  • 增加了有关高阶函数参数协方差的全新部分。

返回类型协方差

在Java(> = 5)

[1]
和C
++中可用
[2]
,但在C#中不支持(Eric
Lippert解释了为什么不这样做以及您可以对此做些什么):

class B {    B Clone();}class D: B {    D Clone();}

接口协方差

[3]
-C#支持

BCL将通用

IEnumerable
接口定义为协变的:

IEnumerable<out T> {...}

因此,以下示例是有效的:

class Animal {}class Cat : Animal {}IEnumerable<Cat> cats = ...IEnumerable<Animal> animals = cats;

请注意,

IEnumerable
按定义,is是“只读”的-您不能向其中添加元素。
与此相反,
IList<T>
可以使用
.Add()
以下方法修改其定义:

public interface IEnumerable<out T> : ...  //covariant - notice the 'out' keywordpublic interface IList<T> : ... //invariant

*通过方法组
*委托协方差

[4]

-在C#中受支持

class Animal {}class Cat : Animal {}class Prog {    public delegate Animal AnimalHandler();    public static Animal GetAnimal(){...}    public static Cat GetCat(){...}    AnimalHandler animalHandler = GetAnimal;    AnimalHandler catHandler = GetCat;        //covariance}

“纯”委托协方差

[5 - pre-variance-releasearticle]
-在C#中受支持

不带参数但返回某些值的委托的BCL定义是协变的:

public delegate TResult Func<out TResult>()

这允许以下内容:

Func<Cat> getCat = () => new Cat();Func<Animal> getAnimal = getCat;

数组协方差 -在C#中受支持,
以一种破碎的方式

[6]

[7]

string[] strArray = new[] {"aa", "bb"};object[] objArray = strArray;    //covariance: so far, so good//objArray really is an "alias" for strArray (or a pointer, if you wish)//i can haz cat?object cat == new Cat();         //a real cat would object to being... objectified.//now assign itobjArray[1] = cat     //crash, boom, bang//throws ArrayTypeMismatchException

最后- 对于高阶函数,令人惊讶且有些弯腰的
Delegate参数协方差 (是的,这是
方差)。

[8]

接受一个参数但不返回任何值的委托人的BCL定义是 不变的

public delegate void Action<in T>(T obj)

忍受我。让我们定义一个马戏团动物训练师-可以告诉他 如何 训练动物(给他一个

Action
可以和该动物一起工作的动物)。

delegate void Trainer<out T>(Action<T> trainingAction);

我们有培训师的定义,让我们找一个培训师让他上班。

Trainer<Cat> catTrainer = (catAction) => catAction(new Cat());Trainer<Animal> animalTrainer = catTrainer;  // covariant: Animal > Cat => Trainer<Animal> > Trainer<Cat>//define a default training methodAction<Animal> trainAnimal = (animal) =>    {    Console.WriteLine("Training " + animal.GetType().Name + " to ignore you... done!");    };//work it!animalTrainer(trainAnimal);

输出证明这可行:

训练Cat忽略您…完成!

为了理解这一点,开个玩笑是有必要的。

一位语言学教授有一天在上课。
他说:“用英语来说,双重否定构成肯定。
但是,没有语言可以使双重否定形成否定。”

房间后面传来一个声音,“是的,对。”

什么 必须做的协方差?

让我尝试一下餐巾纸示范。

an

Action<T>
是互变的,即它“翻转”类型的关系:

A < B => Action<A> > Action<B> (1)

更改

A
B
上面
Action<A>
Action<B>
并获得:

Action<A> < Action<B> => Action<Action<A>> > Action<Action<B>>or (flip both relationships)Action<A> > Action<B> => Action<Action<A>> < Action<Action<B>> (2)

将(1)和(2)放在一起,我们得到:

,-------------(1)--------------. A < B => Action<A> > Action<B> => Action<Action<A>> < Action<Action<B>> (4)         `-------------------------------(2)----------------------------'

但是我们的

Trainer<T>
代表实际上是
Action<Action<T>>

Trainer<T> == Action<Action<T>> (3)

因此我们可以将(4)重写为:

A < B => ... => Trainer<A> < Trainer<B>

-根据定义,它表示Trainer是协变的。

简而言之,应用

Action
两次 我们就得到了相反的方差,即类型之间的关系被翻转了 两次 (请参阅(4)),因此我们回到了协方差。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/463618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号