"是"与“as”相对 [英] "is" vs. "as"

查看:127
本文介绍了"是"与“as”相对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些方法中的任何一种都有优势,还是仅仅是一种风格的问题?我曾经使用过as方法,但后来放弃了是

方法,因为代码的意图对我来说似乎有点清楚。我现在有一些项目即将到来,其中baseTypeCollection将包含5k到10k

对象,这些对象必须搜索各种子类型,我只是

期望找到~50到约100场比赛。谢谢!


foreach(baseType在baseTypeCollection中)

{

if(baseType is desiredSubType)

{

typeFound =(desiredSubType)baseType;

//使用typeFound做我的事情

}

}

foreach(baseType在baseTypeCollection中)

{

typeFound = baseType as desiredSubType;

if(typeFound!= null)

{

//用typeFound做我的东西

}

}


-

Bobby C. Jones
www.AcadX.com

Is there an advantage to either of these methods, or is it simply a matter
of style? I used to use the "as" method, but later gave it up for the "is"
method as the intent of the code seems a little clearer to me. I now have
some projects coming up where the baseTypeCollection will contain 5k to 10k
objects that will have to be searched for various sub types of which I only
expect to find ~50 to ~100 matches. Thanks!

foreach (baseType in baseTypeCollection)
{
if (baseType is desiredSubType)
{
typeFound = (desiredSubType) baseType;
//do my thing with typeFound
}
}
foreach (baseType in baseTypeCollection)
{
typeFound = baseType as desiredSubType;
if (typeFound != null)
{
//do my thing with typeFound
}
}

--
Bobby C. Jones
www.AcadX.com

推荐答案

第一个更自然地阅读。

" Bobby C. Jones < bobbyj(at)acadx(dot)com>在消息中写道

新闻:#g ************** @ TK2MSFTNGP10.phx.gbl ...
Well the first one is more natural to read.
"Bobby C. Jones" <bobbyj (at) acadx (dot) com> wrote in message
news:#g**************@TK2MSFTNGP10.phx.gbl...
是否有优势这些方法中的任何一种,还是仅仅是风格的问题?我曾经使用过as方法,但后来放弃了
"是&作为代码意图的方法对我来说似乎有点清楚。我现在有一些项目即将到来,其中baseTypeCollection将包含5k到
10k个对象,这些对象必须搜索各种子类型,其中我
只期望找到~50到~100火柴。谢谢!

foreach(baseTypeCollection中的baseType)
{
if(baseType is desiredSubType)
{
typeFound =(desiredSubType)baseType;
//使用typeFound做我的事情
}

foreach(baseTypeCollection中的baseType)
{
typeFound = baseType as desiredSubType;
if(typeFound!= null)
//
//用typeFound做我的事
}
}

- Bobby C .Jones
www.AcadX.com
Is there an advantage to either of these methods, or is it simply a matter
of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a little clearer to me. I now have
some projects coming up where the baseTypeCollection will contain 5k to 10k objects that will have to be searched for various sub types of which I only expect to find ~50 to ~100 matches. Thanks!

foreach (baseType in baseTypeCollection)
{
if (baseType is desiredSubType)
{
typeFound = (desiredSubType) baseType;
//do my thing with typeFound
}
}
foreach (baseType in baseTypeCollection)
{
typeFound = baseType as desiredSubType;
if (typeFound != null)
{
//do my thing with typeFound
}
}

--
Bobby C. Jones
www.AcadX.com



嗨Bobby,


" Bobby C. Jones" < bobbyj(at)acadx(dot)com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Hi Bobby,

"Bobby C. Jones" <bobbyj (at) acadx (dot) com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
是否有这些方法中的任何一种都有优势,还是仅仅是风格的问题?我曾经使用过as方法,但后来放弃了
"是&作为代码意图的方法对我来说似乎有点清楚。我现在有一些项目即将到来,其中baseTypeCollection将包含5k到
10k个对象,这些对象必须搜索各种子类型,其中我
只期望找到~50到~100火柴。谢谢!

foreach(baseTypeCollection中的baseType)
{
if(baseType is desiredSubType)
{
typeFound =(desiredSubType)baseType;
//使用typeFound做我的事情
}

foreach(baseTypeCollection中的baseType)
{
typeFound = baseType as desiredSubType;
if(typeFound!= null)
{
使用typeFound做我的事情
}
}
Is there an advantage to either of these methods, or is it simply a matter
of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a little clearer to me. I now have
some projects coming up where the baseTypeCollection will contain 5k to 10k objects that will have to be searched for various sub types of which I only expect to find ~50 to ~100 matches. Thanks!

foreach (baseType in baseTypeCollection)
{
if (baseType is desiredSubType)
{
typeFound = (desiredSubType) baseType;
//do my thing with typeFound
}
}
foreach (baseType in baseTypeCollection)
{
typeFound = baseType as desiredSubType;
if (typeFound != null)
{
//do my thing with typeFound
}
}




使用as;比使用是更具表现形式。因为只有一个

而不是两个。在大多数情况下,我会说区别是

可以忽略不计,但考虑到你的情况,你可能只是注意到差异。


问候,

Dan



Using "as" is more performat than using "is" because there is only one
cast instead of two. In most scenarios I would say the difference is
negligable, but given your scenario, you may just notice a difference.

Regards,
Dan


Bobby,


在上一篇文章中,Greg Ewing发现在与
进行比较时
,比使用as检查快约10倍。或使用GetType / typeof进行平等

操作。


但是,对于你正在做的事情,我认为你可以迈出一步

进一步。我认为你应该做的是在你的收藏中,有一个

哈希表。此哈希表应该键入集合中保留的项目类型。哈希表中的值是该类型的

项的集合。


因此,当您添加元素时,您将检查类型。然后查看针对该类型的

哈希表。获取该类型的项目集合,并且

在那里添加对集合中新值的引用。另外,确保

在删除时也这样做。


然后,当你想要查询特定的元素时

类型,您不必通过如此大量的项目进行枚举。

当然,插入和删除会慢一点,但是

整体而言,如果你经常选择这种选择,它应该会增加整体课程的效率。


希望这会有所帮助。 />
-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Bobby C. Jones" < bobbyj(at)acadx(dot)com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Bobby,

In a previous post, Greg Ewing found that when doing comparisons with
is, it is about 10X faster then checking using "as" or doing an equality
operation with GetType/typeof.

However, for what you are doing, I think that you can go one step
further. I think that what you should do is in your collection, have a
hashtable. This hashtable should be keyed on the type of the items that are
kept in the collection. The values in the hashtable are collections of the
items of that type.

So, when you add an element, you would check the type. Then look in the
hashtable against that type. Get the collection of items of that type, and
add a reference to the new value in your collection there. Also, make sure
to do the same when deleting.

Then, when you want to query for the elements that are of a particular
type, you don''t have to enumerate through such a large number of items.
Granted, the insert and deletes are going to be a little slower, but
overall, if you do this kind of select often, it should increase the overall
efficiency of the class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Bobby C. Jones" <bobbyj (at) acadx (dot) com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
是否有这些方法中的任何一种都有优势,还是仅仅是风格的问题?我曾经使用过as方法,但后来放弃了
"是&作为代码意图的方法对我来说似乎有点清楚。我现在有一些项目即将到来,其中baseTypeCollection将包含5k到
10k个对象,这些对象必须搜索各种子类型,其中我
只期望找到~50到~100火柴。谢谢!

foreach(baseTypeCollection中的baseType)
{
if(baseType is desiredSubType)
{
typeFound =(desiredSubType)baseType;
//使用typeFound做我的事情
}

foreach(baseTypeCollection中的baseType)
{
typeFound = baseType as desiredSubType;
if(typeFound!= null)
//
//用typeFound做我的事
}
}

- Bobby C .Jones
www.AcadX.com
Is there an advantage to either of these methods, or is it simply a matter
of style? I used to use the "as" method, but later gave it up for the "is" method as the intent of the code seems a little clearer to me. I now have
some projects coming up where the baseTypeCollection will contain 5k to 10k objects that will have to be searched for various sub types of which I only expect to find ~50 to ~100 matches. Thanks!

foreach (baseType in baseTypeCollection)
{
if (baseType is desiredSubType)
{
typeFound = (desiredSubType) baseType;
//do my thing with typeFound
}
}
foreach (baseType in baseTypeCollection)
{
typeFound = baseType as desiredSubType;
if (typeFound != null)
{
//do my thing with typeFound
}
}

--
Bobby C. Jones
www.AcadX.com



这篇关于&QUOT;是&QUOT;与“as”相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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