问题描述
你好,
说我有以下两个班级:
班级基础
{
}
类继承:Base
{
}
然后我创建两个数组,并用继承的
实例填充基数:
Inherited [] inheriteds = new Inherited [1 ];
Base [] bases = new Base [1];
bases [0] = new Inherited();
为什么可以进行以下缩小转换:
inheriteds [0] =(继承)bases [0];
但是不可能做以下缩小转换:
inheriteds =(继承[])基础;
此外,为什么是这是运行时错误而不是编译时错误吗?
谢谢,
Erik
Hello,
Say I have the following two classes:
class Base
{
}
class Inherited : Base
{
}
Then I create two arrays of each, and fill the base array with an inherited
instance:
Inherited[] inheriteds = new Inherited[1];
Base[] bases = new Base[1];
bases[0] = new Inherited();
Why is it possible to do the following narrowing conversion:
inheriteds[0] = (Inherited) bases[0];
But NOT possible to do the following narrowing conversion:
inheriteds = (Inherited[]) bases;
Furthermore, why is this a run-time error and not a compile time error?
Thanks,
Erik
推荐答案
有。如果你更换
bases [0] = new Inherited();
with
inheriteds [0] = new Inherited();
bases = inheriteds;
代码成功运行。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
|
请回复到新闻组。
There is. If you replace
bases[0] = new Inherited();
with
inheriteds[0] = new Inherited();
bases = inheriteds;
the code runs successfully.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
有。如果你替换
base [0] = new Inherited();
inheriteds [0] = new Inherited();
bases = inheriteds;
代码成功运行。
Mattias
-
Mattias Sj?gren [MVP] mattias @ mvps.org
|
请仅回复新闻组。
There is. If you replace
bases[0] = new Inherited();
with
inheriteds[0] = new Inherited();
bases = inheriteds;
the code runs successfully.
Mattias
--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
这篇关于缩小与数组的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!