缩小与数组的转换 [英] narrowing conversions with arrays

查看:44
本文介绍了缩小与数组的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


说我有以下两个班级:


班级基础

{

}


类继承: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

推荐答案

我可以理解*为什么*,而不检查数组的每个元素

woul无法确保数组包含类型为

''Inherited''或''Base''的对象 - 它可能包含从
$派生的其他类型的对象b $ b未继承的基础,因此之后访问数组

可能会失败。


令我困惑的是为什么这不是被编译器捕获。

除非* *是*它可以在运行时工作的情况,对我来说似乎像编译器错误一样好吗?

I can understand *why*, without checking each every element of the array it
wouldn''t be possible to ensure that the array contains objects of type
''Inherited'' or ''Base'' -- it may contain objects of other types derived from
Base that are not Inherited and therefore accessing the array afterwards
might fail.

The bit that confuses me is why this wouldn''t be caught by the compiler.
Unless there *is* a situation where it could work at runtime, to me to seems
like a compiler bug?


John,
John,
令我困惑的是为什么这不会被编译器捕获。
除非有*可能在运行时工作的情况,对我而言似乎像编译器错误一样?
The bit that confuses me is why this wouldn''t be caught by the compiler.
Unless there *is* a situation where it could work at runtime, to me to seems
like a compiler bug?




有。如果你更换


bases [0] = new Inherited();


with


inheriteds [0] = new Inherited();

bases = inheriteds;


代码成功运行。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



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.


我认为他的观点是为什么:


inheriteds =(Inherited [])bases;


导致运行时错误,而不是编译时错误。我想他知道如何让b $ b让它上班......只是好奇吗?!


" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

news:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
I think his point was why this:

inheriteds = (Inherited[]) bases;

causes a runtime error and not a compile time error. I think he knows how to
get it to work... was just curious?!

"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
John,
John,
让我感到困惑的是为什么编译器不会抓住它。
除非* *是*它可以在运行时工作的情况,对我来说
似乎是一个编译器错误?
The bit that confuses me is why this wouldn''t be caught by the compiler.
Unless there *is* a situation where it could work at runtime, to me to seemslike a compiler bug?



有。如果你替换

base [0] = new Inherited();



inheriteds [0] = new Inherited();
bases = inheriteds;

代码成功运行。

Mattias

-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请仅回复新闻组。



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.



这篇关于缩小与数组的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆