foreach被认为是危险的? [英] foreach considered dangerous?

查看:89
本文介绍了foreach被认为是危险的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach会在没有警告的情况下隐式地将集合中的每个对象强制转换为指定的

taget类型。

如果没有泛型,这种行为的优势在于我们可以减少输入/>
因为几乎每个集合都需要进行铸造。但是使用泛型我认为这个foreach的特性是危险的。铸造现在几乎总是不需要。


界面IFoo {}

class Foo:IFoo {}

class FooBar:IFoo {}


IFoo [] fooarray = {new Foo()};


foreach(Fooarray中的FooBar )// oops InvalidCastException

{

}


我的建议现在是foreach应该在将来发出编译器警告,如果

会发生隐式演员。


-

cody


免费软件工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk

解决方案

Cody:


我不确定我是否遵循你的论点,但我想理解它

更好。无论是否使用

泛型,您创建的代码示例都是无效的。我只在代码中看到一个显式的强制转换(从技术上讲,它可能是从对象到FooBar的
,因为枚举器可能会返回

对象)

cody <无**************** @ gmx.net>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

foreach确实隐式地将集合中的每个对象强制转换为
指定的taget类型,而不会发出警告。
如果没有泛型,这种行为的优势在于我们不需要输入类型
因为几乎每个集合都需要进行转换。但是使用泛型
我认为foreach的这个特性是危险的。铸造现在几乎是
总是不需要的。

接口IFoo {}
类Foo:IFoo {}
类FooBar:IFoo {}
IFoo [] fooarray = {new Foo()};

foreach(Fooarray中的FooBar)// oops InvalidCastException
{
}

我的提议现在是foreach应该在将来发出编译器警告
如果发生隐式转换。

-


免费软件工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk


>我不确定我是否按照你的论点,但我想更好地理解它

。您创建的代码示例无效,或者没有泛型的
。我只在代码中看到一个显式的强制转换(从技术上讲,它可能是从对象到FooBar,因为枚举器可能会返回
对象)



请重读我的帖子。有一个从Foo到Foobar的隐式演员与

将导致异常。


但是看看foreach是多么危险:即使指出人们做的问题

看不到无效的演员:)


-

cody


[免费软件,游戏和幽默]
www.deutronium.de.vu || www.deutronium.tk


J.


那里没有变量,但这会编译并炸毁

IFoo [] fooarray = {new Foo()};

foreach(Fooarray中的FooBar f)// oops InvalidCastException


{

}


" J.Marsch" < JE **** @ ctcdeveloper.com>在消息中写道

新闻:OS ************** @ tk2msftngp13.phx.gbl ...

Cody:

我不确定我是否遵循你的论点,但我想更好地理解它。您创建的代码示例无效,或者没有泛型的
。我只在代码中看到一个显式的强制转换(从技术上讲,它可能是从对象到FooBar,因为枚举器可能会返回
对象)

cody <无**************** @ gmx.net>在消息中写道
新闻:%2 **************** @ tk2msftngp13.phx.gbl ...

foreach会隐式转换每个对象在集合中指定的

taget类型,没有警告。
没有泛型,这种行为的优势在于我们打字的次数较少
因为几乎每个集合都需要进行拼接。但是使用



泛型我

认为foreach的这个特性是危险的。铸造现在几乎


总是

不需要。

接口IFoo {}
类Foo:IFoo {}
类FooBar:IFoo {}

IFoo [] fooarray = {new Foo()};

foreach(fooarray中的FooBar)// oops InvalidCastException
{
}

我的提议现在是foreach应该在将来发出编译器警告


如果

会发生隐式转换。

-
cody

免费工具,游戏和幽默
http://www.deutronium.de.vu || http://www.deutronium.tk



foreach does implicitly cast every object in the collection to the specified
taget type without warning.
Without generics this behaviour had the advantage of less typing for us
since casting was neccessary in nearly every collection. But with generics I
consider this feature of foreach as dangerous. Casting is now almost always
unwanted.

interface IFoo{}
class Foo:IFoo{}
class FooBar:IFoo{}

IFoo[] fooarray = {new Foo()} ;

foreach (FooBar in fooarray) // oops InvalidCastException
{
}

My proposal is now that foreach should in future emit a compiler warning if
implicit cast would occur.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk

解决方案

Cody:

I''m not sure that I follow your argument, but I''d like to understand it
better. The code example that you created would be invalid with or without
generics. I only see an explicit cast in the code (technically, it''s
probably from object to FooBar, because the enumerator probably returns
object)
"cody" <no****************@gmx.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

foreach does implicitly cast every object in the collection to the specified taget type without warning.
Without generics this behaviour had the advantage of less typing for us
since casting was neccessary in nearly every collection. But with generics I consider this feature of foreach as dangerous. Casting is now almost always unwanted.

interface IFoo{}
class Foo:IFoo{}
class FooBar:IFoo{}

IFoo[] fooarray = {new Foo()} ;

foreach (FooBar in fooarray) // oops InvalidCastException
{
}

My proposal is now that foreach should in future emit a compiler warning if implicit cast would occur.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk



> I''m not sure that I follow your argument, but I''d like to understand it

better. The code example that you created would be invalid with or without generics. I only see an explicit cast in the code (technically, it''s
probably from object to FooBar, because the enumerator probably returns
object)


Please reread my posting. there is an implicit cast from Foo to Foobar with
will cause an exception.

But see how dangerous foreach is: even if pointed to the problem people do
not see the invalid cast :)

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


J.

There''s not a variable there, but this compiles and blows up
IFoo[] fooarray = {new Foo()} ;
foreach (FooBar f in fooarray) // oops InvalidCastException

{
}

"J.Marsch" <je****@ctcdeveloper.com> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...

Cody:

I''m not sure that I follow your argument, but I''d like to understand it
better. The code example that you created would be invalid with or without generics. I only see an explicit cast in the code (technically, it''s
probably from object to FooBar, because the enumerator probably returns
object)
"cody" <no****************@gmx.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

foreach does implicitly cast every object in the collection to the specified

taget type without warning.
Without generics this behaviour had the advantage of less typing for us
since casting was neccessary in nearly every collection. But with


generics I

consider this feature of foreach as dangerous. Casting is now almost


always

unwanted.

interface IFoo{}
class Foo:IFoo{}
class FooBar:IFoo{}

IFoo[] fooarray = {new Foo()} ;

foreach (FooBar in fooarray) // oops InvalidCastException
{
}

My proposal is now that foreach should in future emit a compiler warning


if

implicit cast would occur.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk




这篇关于foreach被认为是危险的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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