本文介绍了转换为泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 考虑到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中是否有设置会显示编译时我不必要的演员 ? 谢谢。 解决方案 要回答这个问题以及其他类似的问题,你可以使用和不使用强制转换编译代码 然后使用ILDASM.exe来检查编译器生成的MSIL是否为。这将证明演员是否已经优化或者不优化。 要回答这个问题以及其他类似的问题,你可以编译代码 有无强制转换,然后使用ILDASM.exe来检查编译器生成的MSIL 。这将证明演员已经优化了或者没有优化。 是的,编译器足够聪明。我刚检查过。 Given the inneficiencies of ArrayList and Hashtable on 64-bit systems, Iam converting them to List<and Dictionary<respectively. It''s apretty 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 castBut, 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 isfrom a strongly typed list:Customer c = (Customer) aaa[0];So changing declarations to generics was pretty easy, but the there areabout 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 castswhen compiling?Thanks. 解决方案To answer this, and other similar questions, you can compile the codewith and without the cast and then use ILDASM.exe to examine the MSIL thatthe compiler has generated. This will demonstrate wether the cast has beenoptimized away or not. To answer this, and other similar questions, you can compile the codewith and without the cast and then use ILDASM.exe to examine the MSILthat the compiler has generated. This will demonstrate wether the casthas been optimized away or not.Yep, the compiler is smart enough. I just checked. 这篇关于转换为泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 05:00