Avoid repeated class instantiation, Singleton Mode

Source: Internet
Author: User

If you have any questions, add the QQ group.14670545Discussion

When calling a method in a class, if the method is not of the static type, we will instantiate the Class Object multiple times and then call it. The following figure shows the singleton mode.

Write the following similar code in this class:

Public class entrantattachmentsclass {# region instance Private Static readonly object lockhelper = new object (); Private Static entrantattachmentsclass _ instance; public static entrantattachmentsclass instance {get {If (_ instance = NULL) lock (lockhelper) {_ instance = new entrantattachmentsclass ();} return _ instance ;}# endregion public int car_add (entrantattachmentsmodel) {.......}...}

In this way, we can easily make the following calls:

Entrantattachmentsclass. instance. car_add (_ Model)> 0;

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.