Talking about:arraylist,list<t>,list<object>

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.