


Implementation of C# singleton pattern and examples of performance comparison
This article mainly introduces relevant information about the implementation and performance comparison of C# singleton mode. It introduces 6 implementation methods in detail. Friends in need can refer to the following
Introduction
A singleton refers to a class that can only have one instance (in C#, more accurately, it is a class that can only have one instance in each AppDomain. It is used in software engineering One of the most common modes. After the first user creates an instance of this class, subsequent users who need to use this class can only use the previously created instance and cannot create a new instance. A singleton is created when it is used for the first time. This article will introduce several singleton implementation methods in C# and analyze the thread safety and performance differences between them. There are many, but from the simplest implementation (non-lazy-loading, non-thread-safe, and inefficient) to a lazily-loadable, thread-safe, and efficient implementation, they all have some basic things in common:
- Singleton classes have only one private parameterless constructor
- The class is declared sealed (not required)
- There is a static variable in the class that holds a reference to the created instance
- The singleton class will provide a static method or property to return a reference to the created instance (eg.GetInstance)
One non-thread safety
//Bad code! Do not use!
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
#. ##Second Simple Thread Safety Implementation
public sealed class Singleton { private static Singleton instance = null; private static readonly object padlock = new object(); Singleton() { } public static Singleton Instance { get { lock (padlock) { if (instance == null) { instance = new Singleton(); } return instance; } } } }
Note: In C#, the same thread can lock an object multiple times. , but if different threads are locked at the same time, thread waiting may occur, or serious deadlock may occur. Therefore, when we use lock, try to lock private variables in the class, so as to avoid the above situation.
Triple double verification thread safety implementationpublic sealed calss Singleton { private static Singleton instance = null; private static readonly object padlock = new object(); Singleton() { } public static Singleton Instance { get { if (instance == null) { lock (padlock) { if (instance == null) { instance = new Singleton(); } } } return instance; } } }
While ensuring thread safety, this implementation also avoids the lock operation every time Instance is called, which will save money A certain amount of time. However, this implementation also has its disadvantages:
1 does not work in Java. (The specific reasons can be found in the original text, I don’t understand much here)
2 Programmers can easily make mistakes when implementing it themselves. If you make your own modifications to the code in this mode, be very careful because the logic of double check is relatively complex and it is easy to make mistakes due to poor thinking.Four thread-safe implementations without locks
public sealed class Singleton { //在Singleton第一次被调用时会执行instance的初始化 private static readonly Singleton instance = new Singleton(); //Explicit static consturctor to tell C# compiler //not to mark type as beforefieldinit static Singleton() { } private Singleton() { } public static Singleton Instance { get { return instance; } } }
This implementation is very simple and does not use locks, but it is still thread-safe. A static, readonly Singleton instance is used here. It will create a new instance when the Singleton is called for the first time. The thread safety guarantee when creating the new instance is directly controlled by .NET. We can think of it as an atomic operation. And it will only be created once in an AppDomaing.
This implementation also has some shortcomings:1The timing of instance being created is unknown, and any call to Singleton will create the instance in advance2The cyclic call of the static constructor . If there are two classes, A and B, and B is called in the static constructor of A, and A is called in the static constructor of B, these two will form a circular call, which will seriously cause the program to crash. 3 We need to manually add the static constructor of Singleton to ensure that the Singleton type will not be automatically added with the beforefieldinit Attribute to ensure that the instance will be created when Singleton is called for the first time.
4 The readonly attribute cannot be changed at runtime. If we need to dispose the instance and recreate a new instance when the program is running, this implementation method cannot be satisfied.
Five fully lazy loading implementation (fully lazy instantiation)
public sealed class Singleton { private Singleton() { } public static Singleton Instance { get { return Nested.instance; } } private class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly Singleton instance = new Singleton(); } }
Implementation five is a wrapper for implementation four. It ensures that instance will only be called in the get method of Instance and will only be initialized before the first call. It is a version of implementation 4 that ensures lazy loading.
6 Use .NET4's Lazypublic sealed class Singleton { private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }
.NET4 or above version supports Lazy Performance difference In the previous implementation, we emphasized the thread safety and lazy loading of the code. However, in actual use, if the initialization of your singleton class is not a time-consuming operation or the initialization sequence does not cause bugs, delayed initialization is a dispensable feature because the time taken by the initialization is negligible. of. In actual usage scenarios, if your singleton instance will be called frequently (such as in a loop), then the performance consumption caused by ensuring thread safety is more worthy of attention. In order to compare the performance of these implementations, I did a small test, looping through the singletons in these implementations 900 million times, calling the instance method each time to perform a count++ operation, and every one million Output once, the running environment is Visual Studio for Mac on MBP. The results are as follows: The test method is not rigorous, but it can still be seen that method two has The need to call lock is the most time-consuming, almost three times as long as the others. Ranked second is the implementation using the .NET Lazy type, which is about one-half more than the others. The remaining four have no obvious differences. Summary Generally speaking, the various singleton implementation methods mentioned above are not very different under today's computer performance, unless you need a particularly large amount of concurrency. Only when you call instance will you need to consider lock performance issues. For ordinary developers, it is good enough to use method 2 or method 6 to implement singletons. Methods 4 and 5 require a good understanding of the C# running process and implementation They require certain skills, and the time they save is still limited. Quote Most of this article is translated from Implementing the Singleton Pattern in C#, with some of my own understanding added. This is what I saw when I searched for static readonly field initializer vs static constructor initialization. I would like to express my thanks to the two authors here.
Thread safety
Lazy loading
Test running time (ms)
Realize one
No
Yes
15532
Realization Two
Yes
Yes
45803
Realization Three
Yes is
15953
realizes four
is
incomplete
14572
realize five
是
是
14295
realize six
是
是
22875
The above is the detailed content of Implementation of C# singleton pattern and examples of performance comparison. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

If you are a .NET developer, you must be aware of the importance of optimizing functionality and performance in delivering high-quality software. By making expert use of the provided resources and reducing website load times, you not only create a pleasant experience for your users but also reduce infrastructure costs.

In terms of high-concurrency request processing, .NETASP.NETCoreWebAPI performs better than JavaSpringMVC. The reasons include: AOT early compilation, which reduces startup time; more refined memory management, where developers are responsible for allocating and releasing object memory.

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.
