Why are C# arrays covariant and what benefits does it bring?

前端 未结 3 1215
终归单人心
终归单人心 2021-01-12 05:48

I\'m having trouble understanding why arrays in C# are covariant and what benefits this covariance can bring. Consider the following trivial code example:

ob         


        
3条回答
  •  臣服心动
    2021-01-12 06:31

    Eric Lippert says:

    Unfortunately, this particular kind of covariance is broken. It was added to the CLR because Java requires it and the CLR designers wanted to be able to support Java-like languages. We then up and added it to C# because it was in the CLR. This decision was quite controversial at the time and I am not very happy about it, but there’s nothing we can do about it now.

提交回复
热议问题