In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the implementation code of singleton pattern in C#". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the implementation code of singleton pattern in C#"!
The hungry Chinese implementation is simple and immediately instantiated in the static constructor:
Public class Singleton
{
Private static readonly Singleton _ instance
Static Singleton ()
{
_ instance = new Singleton ()
}
Public static Singleton Instance
{
Get
{
Return _ instance
}
}
}
Note that to ensure singleton, you need to use the readonly keyword to declare that the instance cannot be modified.
The above words can be abbreviated as:
Public class Singleton
{
Private static readonly Singleton _ instance = new Singleton ()
Public static Singleton Instance
{
Get
{
Return _ instance
}
}
}
New Singleton () here is equivalent to instantiation in a static constructor. In C # 7, it can be further abbreviated as follows:
Public class Singleton
{
Public static Singleton Instance {get;} = new Singleton ()
}
One sentence of code is done, and this instantiation is also done in the default static constructor. If it is hungry Han style demand, this kind of realization is the simplest. Someone will ask if there is a thread safety problem, and if multiple threads call Singleton.Instance at the same time, multiple instances will be instantiated. No, because CLR ensures that all static constructors are thread-safe.
Note that you can't write like this:
Public class Singleton
{
Public static Singleton Instance = > new Singleton ()
}
/ / equivalent to:
Public class Singleton
{
Public static Singleton Instance
{
Get {return new Singleton ();}
}
}
This causes a new instance to be created for each call.
Lazy style
Lazy singleton implementation needs to consider thread safety. First, let's take a look at a classic thread-safe single-column mode implementation code:
Public sealed class Singleton
{
Private static volatile Singleton _ instance
Private static readonly object _ lockObject = new Object ()
Public static Singleton Instance
{
Get
{
If (_ instance = = null)
{
Lock (_ lockObject)
{
If (_ instance = = null)
{
_ instance = new Singleton ()
}
}
}
Return _ instance
}
}
}
Online search for C# singleton mode, most of them are written using lock to ensure thread safety, this is the classic standard singleton mode writing, no problem, very rest assured. In lock both inside and outside to do an instance null judgment, double insurance, enough to ensure thread safety and singleton. But this way of writing seems to be too troublesome and easy to make mistakes. Back in C # 3.5, there was a better way to write it, using Lazy.
Sample code:
Public class LazySingleton
{
Private static readonly Lazy _ instance =
New Lazy () = > new LazySingleton ()
Public static LazySingleton Instance
{
Get {return _ instance.Value;}
}
}
Call example:
Public class Program
{
Public static void Main ()
{
Var instance = LazySingleton.Instance
}
}
Using Lazy, you can delay the instantiation of an object until it is called for the first time, create and get an instance by accessing its Value property, and reading the Value property of an Lazy instance executes the instantiated code only once, ensuring thread safety.
At this point, I believe you have a deeper understanding of "the implementation code of singleton pattern in C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.