C # Basic Knowledge series 10 (SET)

Source: Internet
Author: User

This section mainly aims to understand the Learning set to facilitate the selection of the set during programming and make the program more robust. Before learning about a collection, you must first understand some data structure knowledge. Next, let's take a brief look at the data structure.

A Data Structure is a set of data elements that have one or more specific relationships with each other. If the programming world is a little classic, Program Design = Data Structure + algorithm. It is embodied in source code. The data structure is programming.

  

 

We can see that the set is divided into linear sets and non-linear sets. Linear sets are classified into direct storage and sequential Storage Based on storage methods.

  Direct StorageIt means that the data elements of this type of set can be accessed directly by subscript (index). There are three types of data structures directly stored in C: array (including Array and List <T>), string, and struct.

Direct storage structure has the following advantages:It is very efficient to add elements to the data structure. You can simply put them on the first vacant space at the end of the data.

The disadvantage of direct storage structure is:Inserting an element into a set will become inefficient. It needs to make room for the inserted elements and move them in sequence.

Sequential storage structure, that is, linear table.A linear table can be dynamically expanded or reduced to store data elements in a continuous area. A linear table cannot be searched by index. it searches for elements by referencing addresses. To find an element, it must traverse all elements until it finds the corresponding element. Therefore, a linear table has a high efficiency in data insertion and deletion. The disadvantage is that the query efficiency is relatively low.

Linear tables can be divided into queues, stacks, and index clusters. in C #, they are: Queue <T>, Stack <T>, the Index Cluster is further generalized to Dictionary <TKey, TValue> and two-way linked list <T>.

The non-linear set itself is rarely used in practical applications and feels complicated. Therefore, we will not discuss it here. Next, let's take a look at a set of frequently-used items.

An array contains multiple elements of the same type.

Array declaration: int [] intArray;

Note: When declaring an array, square brackets ([]) must follow the type, rather than the variable name. In C #, placing square brackets after the variable name is invalid syntax.

Array initialization:

We know that arrays are of reference type, so we need to allocate them memory on the stack.

1. intArray = new int [3];

2. intArray = new int [] {1, 2, 3 };

3. int [] intArray = {1, 2, 3 };

After the array is declared and initialized, you can use the indexer for access. The indexer always starts with 0, indicating the first element.

Multi-dimensional array:

Generally, it can be a one-dimensional array, a two-dimensional array, a three-dimensional array, or a multi-dimensional array. Let's take a look at the array below.

                        [] intArray = { , ,             [,] intTwoArray= [,, ] = , ] = , ] = , ] = , ] = , ] =             [,] intTwoArray2 =  , , , , , , , ,             =  ,}, {,}, {,,}, {,}, {,,}, {,}, {,,}, {,}, {,

The preceding section briefly describes one-dimensional arrays, two-dimensional arrays, and three-dimensional arrays.

The ArrayList class inherits the following interfaces:

      ArrayList : IList, ICollection, IEnumerable, ICloneable

The following describes the basic operations of ArrayList.

                        [] intArray = { , ,             ArrayList array =             array.Add(            array.Insert(,             array.Remove(            array.RemoveAt(            array.RemoveRange(,              ( i             array.IndexOf(            array.Contains(

The List <T> class is the generic equivalent class of the ArrayList class. This class implements the IList <T> generic interface using an array that can be dynamically increased as needed.

See the interfaces inherited by List <T>.

       (Mscorlib_CollectionDebugView<>  List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

T is the type of the elements in the List. The following uses string as an example to describe the basic usage of List <T>. During the test, we found that the List <T> operation is basically similar to that of ArrayList. They also inherit the IList, ICollection, and IEnumerable interfaces.

T can also be a custom type, which is also the most widely used in daily programming. First, define an object.

       FirstName { ;   LastName { ; 

Next, perform the definition and initialization operations.

            List<Person> list =  List<Person> Person() { FirstName=,LastName= Person() { FirstName = , LastName =  });

Because the operation is basically the same as the ArrayList above, we will not introduce it too much here.

1. Common usage of Dictionary

Dictionary <string, string> is a generic type. It has a set function. Sometimes it can be seen as an array. Its structure is as follows: Dictionary <[key], [value]>. It features that the stored object needs to be saved to the generic type one-to-one correspondence with the [key] value, find the corresponding value through a certain [key.

          Main(            Dictionary<, > dictionaryList =  Dictionary<, >            dictionaryList.Add(, , ,              (dictionaryList.ContainsKey(,             str=dictionaryList[             ( key              ( value              ( dic             dictionaryList.Remove(

2. Use the value of dictionary <key, value> As an array.

            Dictionary<,[]> stringList= Dictionary<,[]>, []{,,,, []{,, + stringList[][]);

3. Use the value of Dictionary <key, value> as an entity class.

       FirstName { ;   LastName { ;   Main(<,Person> personList= Dictionary<,Person>=  ( i = ; i < ; i++= = =  ( student 

4, Article space is limited, about dictionary extension method is not introduced here for the moment, if you are interested can see the great God works http://www.cnblogs.com/ldp615/archive/2011/01/28/dictionary-extensions.html

In fact, we can find that their basic operations are similar, that is, they have inherited common interfaces. Here is a brief introduction to the use of the most common collections.

 

Tips for English

Don't be silly. -- Don't be confused.

I am not available. -- I am busy

There is nothing I can do. -- I can't do anything.

Everything will be okey in the end. If it's not okey, it's not the end. -- Everything is always good at the end. If it is not good, it means it is not till the end.

Author: aehyok

Source: http://www.cnblogs.com/aehyok/

Thank you for reading this article. If you are interested in the content described in my blog, please make a suggestion. Thank you for your support:-O.

 

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.