Comprehensive comparison of arrays, collections (ArrayList), generic collections list<t>, dictionaries (dictionary<tkey,tvalue>) in C #

Source: Internet
Author: User

Comprehensive comparison of arrays, collections (ArrayList), generic collections list<t>, dictionaries (dictionary<tkey,tvalue>) in C #

Why put these 4 things together, because these 4 objects in C # are a collection of data to store ....

First of all, let's declare and instantiate these 4 objects:

//Array

string[] m_str = new String[5];

//Collection

ArrayList m_alist = new ArrayList ();

//Generic collection

list<int> m_list = new list<int> ();

//Dictionary

dictionary<int, string> m_dt = new Dictionary<int, string> ();

Let's see what the similarities and differences between these 4 objects look like in each other.

I give you a wake up to see if they all have the NEW keyword, that is, they all need to instantiate, in the description of the white point they are reference types (do not know what the reference type is not particularly tangled, will specifically say the difference).

All right, let's keep looking.

//Array

string[] m_str = new String[5];

M_str[0] = "a";

M_STR[1] = "a";

M_STR[2] = "a";

1, when declaring an array [] there is a number "5", to this is the difference, the array must specify the length of the declaration. This is because the array is stored continuously in memory, so its index is very fast, and the assignment and modification elements are simple . Another thing is that the array specifies the type when declaring the definition, the type of the array we define is string, and because the array is contiguous, it causes us to want to jump between the first and second elements of the array, and it is inconvenient to insert a member. This leads to the need for a collection of data that can be conveniently used to add/delete/insert the members of the collection. This brings up the collection (ArrayList).

2. Let's look at the 2nd object Set (ArrayList), which is neither size nor type at the time of declaration, what does it mean? Shows that his size is dynamic and you can add/delete/insert like:

Collection

ArrayList m_alist = new ArrayList ();

M_alist.add ("a");

M_alist.add (10);

M_alist.add (TRUE);

M_alist.removeat (0);

M_alist.insert (1, "AA");

You may notice that the members of add in the collection ArrayList have string, numeric, and Boolean values. This illustrates the problem that each member of the collection is of type object, and it packs the specific members into an object and joins it to itself. If you use a member in the collection because the member is of type object, you also want to remove the container to a specific type and then manipulate and use it. This will cause problems:1, consumption performance (frequent unpacking and boxing) 2, unsafe, for example, the first member of the collection object above is a character type, you take out and a numeric variable to operate, will be error .... Then there is another object-the generic collection (List<t>) object.

3, generic collection List<t> object, everyone may see this on the blindfolded, anyway, I first saw is blindfolded, angle brackets is what, T is what? Don't panic, it is not difficult, the angle bracket is a syntax, as T can be understood as a placeholder, it can be string, int, bool ... such as Let's look at the usage of the code:

//Generic collection

list<int> m_list = new list<int> ();

M_list.add (10);

M_list.add (10);

M_list.add (10);

M_list.removeat (0);

M_list.insert (1,12);

It looks like the use of the collection (ArrayList), in fact, is almost, haha. The only difference is that when declaring a generic collection (list<t>), you need to make the type of the member inside, and the reflection set we make is int, which means that the member you are adding must also be an int, what good is it? 1. Data security, you add a string, such as non-int type of members to add not to (compile all through), so that the data is safe and unique, 2. Save the performance, do not need each operation member when the unpacking and packing.

A generic collection is relatively perfect compared to a collection ...

4, finally we talk about the dictionary dictionary<tkey, Tvalue>, everyone looked and blindfolded, don't panic, think about just the generic collection (list<t>)

The same, the angle brackets or the grammar, you follow the good. And this time the placeholder is not T, "because the first one is the index, the second is the specific value content," the index placeholder becomes the TKey, TValue. It's so simple.

Let's look at the syntax:

//Dictionary

dictionary<int, string> m_dt = new Dictionary<int, string> ();

M_dt.add (0, "a");

M_dt.add (1, "B");

M_dt.add (0, "C");

string str=m_dt[0];

Look at the usage is relatively simple, take m_dt.add (0, "a"); In this sentence, 0 is the "index" of the dictionary, and "value" is the string A. To find the specific "value" according to the "index" of the dictionary, the syntax is m_dt[0]; This takes the string a.

  Recall: Because the use of arrays is not convenient, so the collection (ArrayList). Because of the insecure and consumed performance of the collection (ArrayList), there is a generic collection (list<t>). This is the relationship of these 3 people-to compensate for the shortcomings. As for the dictionary this is because it is also a collection of a set of data, and the use of generic things, so put together to say.

Okay, here's the 4 common objects that store a set of data we're done, and we'll see the next chapter on reference types and value types.

Cute Little da ...

Comprehensive comparison of arrays, collections (ArrayList), generic collections list<t>, dictionaries (dictionary<tkey,tvalue>) in C #

Related Article

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.