转换为泛型 [英] Converting to generics

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

问题描述

考虑到64位系统上ArrayList和Hashtable的不足之处,我将分别将它们转换为List<和Dictionary<。这是一个非常庞大的系统,所以有很多演员阵容。例如。


ArrayList aaa = new ArrayList();

aaa.Add(new Customer());

aaa .Add(new Customer());

aaa.Add(new Customer());


客户c =(客户)aaa [0];


使用泛型,它将是这样的:


列表< Customeraaa =新列表< Customer>();

aaa.Add(new Customer());

aaa.Add(new Customer());

aaa.Add(new Customer());


客户c = aaa [0]; //例如。没有演员


但是,鉴于我只是将声明从ArrayList更改为List<>,

,仍然有很多代码都有一个演员表,即使演员从强类型列表中获得


客户c =(客户)aaa [0];

因此,将声明更改为泛型非常简单,但系统中有数十亿美元的演员阵容。所以我有2个问题。


1.编译器是否足够聪明可以取出不必要的演员阵容?

2. VS2005中是否有设置会显示编译时我不必要的演员



谢谢。

解决方案

弗兰克,


我认为编译器可以足够聪明地做到这一点,但是要确保,为什么不编译代码并查看IL?你应该能够很快告诉他们是否正在进行演员表演。


至于找到不必要的演员阵容,没有这样的设置可以做

这适合你。您必须找到(客户)和

的所有实例,然后手动删除它们。


您可以做的是找到并替换所有你的项目和

删除(客户)。然后,当你编译项目时,你会看到编译器告诉你你有一个类型不匹配的地方(因为你假设你是从一个界面进行投射,因此一个是

)或类似的事情。


最后,Hashtable和ArrayList由于他们的b / b $ b泛型对应物因为拳击问题而效率低下,而不是因为他们

在64位系统上运行。如果客户类型是值类型,并且您正在进行这些操作的数量
,您将看到性能增强。你会看到

引用类型的性能增强,但不会那么多。


使用引用类型你仍然希望推动使用泛型所以

你可以得到你的类型用法的编译时检查(而不是因为无效的强制转换而在运行时发现

错误)。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

Frank Rizzo ; < no ** @non.comwrote in message

news:uj ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
鉴于64位系统上ArrayList和Hashtable的不足之处,我将
分别转换为List<和Dictionary<这是一个非常好的b / b
大型系统,所以有很多演员表。例如。


ArrayList aaa = new ArrayList();

aaa.Add(new Customer());

aaa .Add(new Customer());

aaa.Add(new Customer());


客户c =(客户)aaa [0];


使用泛型,它将是这样的:


列表< Customeraaa =新列表< Customer>();

aaa.Add(new Customer());

aaa.Add(new Customer());

aaa.Add(new Customer());


客户c = aaa [0]; //例如。没有演员


但是,鉴于我只是将声明从ArrayList更改为List<>,

,仍然有很多代码都有一个演员表,即使演员来自

a强类型列表:


客户c =(客户)aaa [0];


因此,将声明更改为泛型非常简单,但系统中有数十亿美元的演员阵容。所以我有2个问题。


1.编译器是否足够聪明可以取出不必要的演员阵容?

2. VS2005中是否有设置会显示我编译时不必要的演员




谢谢。



" Frank Rizzo" < no ** @non.comwrote in message

news:uj ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
1.编译器足够智能取出不必要的强制转换?



要回答这个问题以及其他类似的问题,你可以使用和不使用强制转换编译代码

然后使用ILDASM.exe来检查编译器生成的MSIL是否为
。这将证明演员是否已经优化或者不优化。


Alberto Poblacion写道:


Frank Rizzo < no ** @non.comwrote in message

news:uj ************** @ TK2MSFTNGP04.phx.gbl ...
< blockquote class =post_quotes>
> 1。编译器足够聪明,可以取出不必要的强制转换吗?



要回答这个问题以及其他类似的问题,你可以编译代码

有无强制转换,然后使用ILDASM.exe来检查编译器生成的MSIL

。这将证明演员已经优化了或者没有优化。



是的,编译器足够聪明。我刚检查过。


Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I
am converting them to List<and Dictionary<respectively. It''s a
pretty massive system, so there are a lot of casts. For instance.

ArrayList aaa = new ArrayList();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = (Customer) aaa[0];

With generics, it would be like this:

List<Customeraaa = new List<Customer>();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = aaa[0]; //e.g. without the cast

But, given that I simply changed declarations from ArrayList to List<>,
there is still a lot of code that has a cast, even though the cast is
from a strongly typed list:

Customer c = (Customer) aaa[0];
So changing declarations to generics was pretty easy, but the there are
about a zillion casts in the system. So I have 2 questions.

1. Is the compiler smart enough take out the unnecessary casts?
2. Is there a setting in VS2005 that will show me the unnecessary casts
when compiling?

Thanks.

解决方案

Frank,

I would think that the compiler could be smart enough to do this, but to
be sure, why not compile that code and look at the IL? You should be able
to tell pretty quickly if a cast is taking place.

As for finding the unnecessary casts, there is no such setting to do
this for you. You will have to find all the instances of (Customer) and
then remove them manually.

What you could do is a find and replace across all of your projects and
remove (Customer). Then, when you compile the project, you will see where
the compiler tells you that you have a type mismatch (because one is
assuming that you are casting from an interface or something like that).

Finally, the Hashtable and ArrayList are inefficient due to their
generic counterparts because of boxing issues, not because they are being
run on 64 bit systems. If the Customer type is a value type, and you have a
number of these operations taking place, you will see a performance
enhancement. You will see a little bit of a performance enhancement with
reference types, but not as much.

With reference types you still want to make the push to use generics so
you can get compile-time checking of your type usage (as opposed to finding
errors at runtime because of invalid casts).

Hope this helps.

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

"Frank Rizzo" <no**@none.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...

Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, I
am converting them to List<and Dictionary<respectively. It''s a pretty
massive system, so there are a lot of casts. For instance.

ArrayList aaa = new ArrayList();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = (Customer) aaa[0];

With generics, it would be like this:

List<Customeraaa = new List<Customer>();
aaa.Add(new Customer());
aaa.Add(new Customer());
aaa.Add(new Customer());

Customer c = aaa[0]; //e.g. without the cast

But, given that I simply changed declarations from ArrayList to List<>,
there is still a lot of code that has a cast, even though the cast is from
a strongly typed list:

Customer c = (Customer) aaa[0];
So changing declarations to generics was pretty easy, but the there are
about a zillion casts in the system. So I have 2 questions.

1. Is the compiler smart enough take out the unnecessary casts?
2. Is there a setting in VS2005 that will show me the unnecessary casts
when compiling?

Thanks.



"Frank Rizzo" <no**@none.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...

1. Is the compiler smart enough take out the unnecessary casts?

To answer this, and other similar questions, you can compile the code
with and without the cast and then use ILDASM.exe to examine the MSIL that
the compiler has generated. This will demonstrate wether the cast has been
optimized away or not.


Alberto Poblacion wrote:

"Frank Rizzo" <no**@none.comwrote in message
news:uj**************@TK2MSFTNGP04.phx.gbl...

>1. Is the compiler smart enough take out the unnecessary casts?


To answer this, and other similar questions, you can compile the code
with and without the cast and then use ILDASM.exe to examine the MSIL
that the compiler has generated. This will demonstrate wether the cast
has been optimized away or not.

Yep, the compiler is smart enough. I just checked.


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

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