Co-variant array conversion from x to y may cause run-time exception
What it means is this
Control[] controls = new LinkLabel[10]; // compile time legal
controls[0] = new TextBox(); // compile time legal, runtime exception
And in more general terms
string[] array = new string[10];
object[] objs = array; // legal at compile time
objs[0] = new Foo(); // again legal, with runtime exception
In C#, you are allowed to reference an array of objects (in your case, LinkLabels) as an array of a base type (in this case, as an array of Controls).
It is also compile time legal to assign another object that is a Control
to the array.
The problem is that the array is not actually an array of Controls.
At runtime, it is still an array of LinkLabels.
As such, the assignment, or write, will throw an exception.
修正方法:
object[] buildings = {"同济科技大厦", "智能建筑大厦"};//不要使用string[]
comboBoxEdit1.Properties.Items.AddRange(buildings);
Covariance and Contravariance in C#, Part Two: Array Covariance
C# implements variance in two ways. Today, the broken way.
Ever since C# 1.0, arrays where the element type is a reference type are covariant. This is perfectly legal:
Animal[] animals = new Giraffe[10];
Since Giraffe is smaller than Animal, and “make an array of” is a covariant operation on types, Giraffe[] is smaller thanAnimal[], so an instance fits into that variable.
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.
Why is this broken? Because it should always be legal to put a Turtle into an array of Animals. With array covariance in the language and runtime you cannot guarantee that an array of Animals can accept a Turtle because the backing store might actually be an array of Giraffes.
This means that we have turned a bug which could be caught by the compiler into one that can only be caught at runtime. This also means that every time you put an object into an array we have to do a run-time check to ensure that the type works out and throw an exception if it doesn’t. That’s potentially expensive if you’re putting a zillion of these things into the array.
Yuck.
Unfortunately, we’re stuck with this now. Giraffe[] is smaller than Animal[], and that’s just the way it goes.
I would like to take this opportunity to clarify some points brought up in comments to Part One.
First, by "subtype" and "supertype" I mean "is on the chain of base classes" for classes and "is on the tree of base interfaces" for interfaces. I do not mean the more general notion of "is substitutable for". And by “bigger than” and “smaller than” I explicitly do NOT mean “is a supertype of” and “is a subtype of”. It is the case that every subclass is smaller than its superclass, yes, but not vice versa. That is, it is not the case that every smaller type is a subtype of its larger type.Giraffe[] is smaller than both Animal[] and System.Array. Clearly Giraffe[] is a subtype of System.Array, but it isemphatically not a subtype of Animal[]. Therefore the “is smaller than” relationship I am defining is more general than the “is a kind of” relationship. I want to draw a distinction between assignment compatibility (smaller than) and inheritance (subtype of).
Next time we’ll discuss a kind of variance that we added to C# 2.0 which is not broken.
Co-variant array conversion from x to y may cause run-time exception的更多相关文章
- delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决
delphi 10.1 berlin datasnap提交clientdataset.delta报:invalid variant type conversion(类型转换错误)问题的解决,需要打这个 ...
- ArrayList to Array Conversion in Java
ArrayList to Array Conversion in Java Following methods can be used for converting ArrayList to Arra ...
- dotnet 数组自动转基类数组提示 Co-variant array conversion 是什么问题
在 C# 的语法,可以提供自动将某个类的数组自动转这个类的基类数组的方法,但是这样的转换在 Resharper 会提示 Co-variant array conversion 这是什么问题? 在 C# ...
- ruby 学习 -- Array --2
定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...
- numpy.array
关于python中的二维数组,主要有list和numpy.array两种. 好吧,其实还有matrices,但它必须是2维的,而numpy arrays (ndarrays) 可以是多维的. 我们主要 ...
- JAVA generic array 泛型数组
在JAVA中是不支持泛型数组的,不能通过 Z[] array=new Z[10] 这样的方式来创建数组,而是使用反射Aarry.newInstance来创建: 具体代码如下: public Z[][] ...
- Variant
class RTL_DELPHIRETURN Variant: public TVarData Variant转换为字符串 System::Variants::VarToStr VariantArra ...
- QT之Variant
QVariant识别类型的注册 QVariant识别类型的注册 QVariant为一个万能的数据类型--可以作为许多类型互相之间进行自动转换.将C++变为弱数据类型成为可能--也是许多控件中用户定义数 ...
- Python Numpy Array
Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...
随机推荐
- [Castle Windsor]学习依赖注入
初次尝试使用Castle Windsor实现依赖注入DI,或者叫做控制反转IOC. 参考: https://github.com/castleproject/Windsor/blob/master/d ...
- IO流07_输入输出流总体系
[javaIO体系中常用的流] [关于字符流和字节流的注意点] 通常,字节流比字符流功能更加强大,因为字节流可以处理所有的二进制文件. 但是字节流来处理字符,又需要将字节转换成字符,增加了编程复杂度. ...
- 在Mac OS X中搭建STM32开发环境(2)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在上一篇文章中,我们在OSX中编译了ST-Link2调试工具,并且简单的对其功能进行了测试 ...
- javascript调用外部wpf的方法
今天因为项目需要在wpf嵌入web页面,然后在web页面调用wpf的方法,于是突击学习了一下,记下备忘.^_^ 1.wpf中新建一个类WpfForScriptingHelper namespace w ...
- HTML5:基本使用
单行文本输入框 type为text表示input元素为一个单行文本框,是input元素的默认表现形式.单行文本输入框支持下面的属性设置. A:设定元素大小 maxlength属性设定用户能够输入的字符 ...
- iOS之RunTime浅谈
首先说一下什么是runtime:RunTime简称运行时.就是系统在运行的时候的一些机制,其中最主要的是消息机制.对于C语言,函数的调用 在编译的时候会决定调用哪个函数( C语言的函数调用请看这里 ) ...
- javascript进阶——分离式DOM脚本编程
编写分离式(unobstrusive)代码意味着对HTML内容的完全分离:数据来自服务器端,javascript代码用来动态化和交互.这种分离的好处是在不同浏览器之间使用是可以完全降级或升级运行,对于 ...
- MAC自带的lnmp
MAC自身带有apache,php. 1.启动apache服务 sudo apachectl start/restart/stop 2.查看php的版本 php -v 3.让apache支持ph ...
- php开学之环境搭建
1. php版本选择 1.1 PHP非线程安全与线程安全版本的选择技巧 1.2 版本区别 PHP的大版本主要分三支:PHP4/PHP5/PHP6 其中,PHP4由于太古老.对QQ支持不力已基本被淘汰, ...
- php的post和get方法
<?php function post($url,$fields) { $fields_string = ''; foreach($fields as $key=>$value) { $f ...