Writing high-quality Code improves the 157 suggestions for C # programs [security of generic sets, set selection, and set]

Source: Internet
Author: User

Collections are inevitable during software development. collections in C # are represented by arrays and several collection classes. Both arrays and collection classes have their respective advantages and disadvantages. How to use a set is a skill we must master in the development process. Don't underestimate these skills. Once an incorrect set or method for the set is used in development, the application will run away from your expectation.

This article has been updated to http://www.cnblogs.com/aehyok/p/3624579.html. This article mainly records the following content:

20. Use a generic set instead of a non-generic set.

21. Select the correct set.

22. Ensure linear security of the Set

Here is an article in http://www.cnblogs.com/aehyok/p/3384637.html that I used to introduce generics. We should try our best to use generic sets. Because Generics do have their advantages:

1. Provides type security and can check errors during compilation.

2. More importantly, in most cases, the performance of a generic set is much higher than that of a non-generic set.

Let's take a look at a simple code to test the performance:

  collectionCount =  Stopwatch watch =   testCount =              GC.WaitForPendingFinalizers();            GC.Collect();            collectionCount = GC.CollectionCount();            watch =  , GC.CollectionCount() - =  temp =  ( i = ; i < testCount; i++= (=  <> list =  List<> temp =  ( i = ; i < testCount; i++==   Main(

The execution result is as follows:

The number of times I tested above is 10000000. We can see that there is a big gap between the two in the number of garbage collection and time consumption, so generic collections have the advantage that non-generic collections cannot surpass. So try to use generic collections in our programs.

Bytes.

If the number of sets is fixed and transformation is not involved, array efficiency is high; otherwise, List <T> is used.

The efficiency of data insertion and deletion is relatively high, and the efficiency of data query is relatively low.

For more information about queues, see http://msdn.microsoft.com/zh-cn/library/System.Collections.Queue (v = vs.80). aspx

For more information about stack, see http://msdn.microsoft.com/zh-cn/library/System.Collections.Stack (v = vs.110). aspx

As mentioned in recommendation 18, a reason why the foreach loop cannot replace the for loop is that the set itself is added or deleted during the iteration process. Porting this scenario to a multi-threaded scenario is the focus of this suggestion: Ensure the thread security of the set. Collection thread security means that when a live deletion element is added to multiple threads, the threads must be synchronized.

Next we will take a more detailed look at the instance, first simple definition of an object class

       Name { ;   Age { ; 
        List<Person> list =  List<Person> Person(){ Name=,Age= Person(){Name=,Age= Person(){Name=,Age= AutoResetEvent autoSet =  AutoResetEvent(  Main(=  Thread(() => ( item +=  Thread(() =>

A Brief Analysis of this Code is to define a List set and then define an AutoRestEvent instance to control the thread.

         ArrayList list =  Person(){ Name=,Age= Person(){Name=,Age= Person(){Name=,Age= AutoResetEvent autoSet =  AutoResetEvent(  Main(=  Thread(() => (Person item  +=  Thread(() =>

The running result is that the thread passes

If you have tried this, you will find that the generic set does not have this attribute to lock. You must create a lock object to complete the synchronization task.

So we can modify the first example in this way.

  List<Person> list =  List<Person> Person(){ Name=,Age= Person(){Name=,Age= Person(){Name=,Age=  SyncObject =   AutoResetEvent autoSet =  AutoResetEvent(  Main(=  Thread(() => ( item  +=  Thread(() =>

 

Tips for English

Blind date -- blind date

Online session-online meeting

This depends on you -- It depends on you

I have a date with you -- I have an appointment with you

Poor guy-poor guy, you can also say (a poor fish)

You look so tall -- You look very tall (describe high, don't use high)

Awesome-Awesome; frightening; terrible; excellent

Awesome -- can also be translated into (So. Diao)

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.