关于铸造 [英] about casting

查看:78
本文介绍了关于铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


这里我有两种不同的方式来使用演员。

这些中的任何一种都比另一种好吗?

或者只是一个品味问题。


newCards.Add((Card)sourceCard.Clone());

newCards.Add( sourceCard.Clone()as Card);


// Tony

Hello!

Here I have two different way to use casting.
Is any of these any better then the other?
Or is it just a question of taste.

newCards.Add((Card)sourceCard.Clone());
newCards.Add(sourceCard.Clone() as Card);

//Tony

推荐答案

Tony Johansson< jo *****************@telia.comwrote:
Tony Johansson <jo*****************@telia.comwrote:

这里我有两种不同的方式来使用强制转换。 />
这些中的任何一个都比另一个好吗?

或者它只是一个品味问题。


newCards.Add((卡)sourceCard.Clone());

newCards.Add(sourceCard.Clone()as Card);
Here I have two different way to use casting.
Is any of these any better then the other?
Or is it just a question of taste.

newCards.Add((Card)sourceCard.Clone());
newCards.Add(sourceCard.Clone() as Card);



使用" as"如果演员表失败则不会抛出异常 - 它只是

会返回null。


使用直接演员也允许用户定义的转换等。


我倾向于使用as;对于价值观我很怀疑 - 当我即将

测试结果时:


对象未知= GetMeSomeData();

string text = unknown as string;

if(text!= null)

{

...

}

我真正使用直接投射,真的希望它成功 - 例如当故障表明存在问题时应该报告为


例外:


对象shouldBeString = GetMeSomeText();

string text =(string)shouldBeString;
< br $>
-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps.com/jon.skeet

C#深度: http://csharpindepth.com

Using "as" doesn''t throw an exception if the cast fails - it just
returns null instead.

Using a direct cast also allows for user-defined conversions etc.

I tend to use "as" for values I''m in doubt about - when I''m about to
test the result:

Object unknown = GetMeSomeData();
string text = unknown as string;
if (text != null)
{
...
}
I use direct casting when I really, really expect it to succeed - i.e.
when a failure indicates a problem which should be reported as an
exception:

Object shouldBeString = GetMeSomeText();
string text = (string) shouldBeString;

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


另一个注释; "如"只能与引用类型一起使用和

Nullable< T>,因为它不能为常规结构返回null。你需要

才能使用是。为了实现同样的目标。


Marc
One other note; "as" can only be used with reference types and
Nullable<T>, as it can''t return null for a regular struct. You''d need
to use "is" and cast to achieve the same.

Marc


Tony,


我使用作为因为它比(卡片)快得多各种铸件。

另外,我认为(车辆为汽车)。颜色看起来比((车)

车辆更清洁。颜色。这是我的偏好 - 其他人可能会认为(汽车)

看起来更好。让我先让我的同胞NG人先说你只应该优惠yada yada yada。我说,你知道的是什么,我正在开发运行200美元的移动设备,我将使用每一个小的速度提升我可以获得特别是它是免费的。


非常重要的是它们与Jon指出的不同,所以

有时使用一个而不是其他。


无论如何,你去吧。


希尔顿


" Tony Johansson" < jo ***************** @ telia.comwrote in message

news:pk ************* @ newsb.telia.net ...
Tony,

I use "as" because it is much faster than the "(Card)" variety of casting.
Also, I think "(vehicle as Car).Color" looks a lot cleaner than "((Car)
vehicle).Color". That''s my preference though - others might think "(Car)"
looks better. Let me preempt my fellow NGers by saying that you should only
optimize yada yada yada. I say, ya know what, I''m developing on mobile
devices running a 200MHz, I''m gonna use every little speed enhancement I can
get especially when it is for free.

Very important to note is that they are different as Jon pointed out, so
sometimes it makes sense to use one instead of the other.

Anyway, there you go.

Hilton

"Tony Johansson" <jo*****************@telia.comwrote in message
news:pk*************@newsb.telia.net...

你好!


这里我有两种不同的使用方式。

这些中的任何一个都比另一个好吗?

或者它只是一个品味问题。


newCards.Add( (卡片)sourceCard.Clone());

newCards.Add(sourceCard.Clone()as Card);


// Tony
Hello!

Here I have two different way to use casting.
Is any of these any better then the other?
Or is it just a question of taste.

newCards.Add((Card)sourceCard.Clone());
newCards.Add(sourceCard.Clone() as Card);

//Tony



这篇关于关于铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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