Recently, there's a place in the project where list<object> is used to think that list<object> can dynamically insert object objects, and ArrayList is also dynamically inserting object objects. I wonder if these two functions are the same, look at the meta-data code as follows:
ArrayList
public class Arraylist:ilist, ICollection, IEnumerable, ICloneable
public virtual int Add (object value);//Add an Object
List<t>
public class list<t>: Ilist<t>, Icollection<t>, Ienumerable<t>, IList, ICollection, IEnumerable
public void Add (T item);//Add an Object
Compare the two, found that the Add method of the two is overridable, and return the current index, one is the void method, T item, when T is an object, the individual think that the difference is not big, but they if the add value type when the boxing operation will be triggered, affecting performance, through the test found ( Test code in the attachment), ArrayList and list<object> both perform the same operation, the same efficiency, whether there is no disassembly box. However, if you use list<t>, it is best not to pass T into object, otherwise lose the meaning of generic T. If you perform an int operation, use List<int>.
In general, the process of developing procedures to avoid the operation of the Disassembly box, frequent unpacking will reduce the efficiency of a lot.
Digression: Before looking at the interview topic, asked the difference between ArrayList and list, feel confused, until now understand, originally asked is such a problem, alas, the program can not only write, also have to think Ah, in addition, F12 see program Metadata is also a good learning method. Attached program run result 650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/57/1D/wKioL1SSNPGRK6ZNAAEAjzE_B7w360.jpg "title = "QQ picture 20141218095444.png" alt= "wkiol1ssnpgrk6znaaeajze_b7w360.jpg"/>, but biased, if run alone ArrayList or List<object > They two results are not small, but put list<object> in the back of ArrayList run, it will appear slower, there is also a procedural problem, it is here. The above is purely personal opinion, if wrong, please do not hesitate to point out.
This article is from the "PZ Technical training base" blog, please be sure to keep this source http://panzi.blog.51cto.com/4738203/1591247
Brief introduction: Arraylist,list<t>,list<object>