溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中單例模式的實(shí)現(xiàn)代碼

發(fā)布時(shí)間:2021-09-09 16:35:23 來源:億速云 閱讀:338 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“C#中單例模式的實(shí)現(xiàn)代碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“C#中單例模式的實(shí)現(xiàn)代碼”吧!

餓漢式實(shí)現(xiàn)很簡(jiǎn)單,在靜態(tài)構(gòu)造函數(shù)中立即進(jìn)行實(shí)例化:

public class Singleton
{
   private static readonly Singleton _instance;
   static Singleton()
   {
       _instance = new Singleton();
   }

   public static Singleton Instance
   {
       get
       {
           return _instance;
       }
   }
}

注意,為了確保單例性,需要使用 readonly 關(guān)鍵字聲明實(shí)例不能被修改。

以上寫法可簡(jiǎn)寫為:

public class Singleton
{
   private static readonly Singleton _instance = new Singleton();
   public static Singleton Instance
   {
       get
       {
           return _instance;
       }
   }
}

這里的 new Singleton() 等同于在靜態(tài)構(gòu)造函數(shù)中實(shí)例化。在 C# 7 中還可以進(jìn)一步簡(jiǎn)寫如下:

public class Singleton
{
   public static Singleton Instance { get; } = new Singleton();
}

一句代碼就搞定了,此寫法,實(shí)例化也是在默認(rèn)的靜態(tài)構(gòu)造函數(shù)中進(jìn)行的。如果是餓漢式需求,這種實(shí)現(xiàn)是最簡(jiǎn)單的。有人會(huì)問這會(huì)不會(huì)有線程安全問題,如果多個(gè)線程同時(shí)調(diào)用 Singleton.Instance 會(huì)不會(huì)實(shí)例化了多個(gè)實(shí)例。不會(huì),因?yàn)?nbsp;CLR 確保了所有靜態(tài)構(gòu)造函數(shù)都是線程安全的。

注意,不能這么寫:

public class Singleton
{
   public static Singleton Instance => new Singleton();
}

// 等同于:
public class Singleton
{
   public static Singleton Instance
   {
       get { return new Singleton(); }
   }
}

這樣會(huì)導(dǎo)致每次調(diào)用都會(huì)創(chuàng)建一個(gè)新實(shí)例。

懶漢式

懶漢式單例實(shí)現(xiàn)需要考慮線程安全問題,先來看一段經(jīng)典的線程安全的單列模式實(shí)現(xiàn)代碼:

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;
       }
   }
}

網(wǎng)上搜索 C# 單例模式,大部分都是這種使用 lock 來確保線程安全的寫法,這是經(jīng)典標(biāo)準(zhǔn)的單例模式的寫法,沒問題,很放心。在 lock 里外都做一次 instance 空判斷,雙保險(xiǎn),足以保證線程安全和單例性。但這種寫法似乎太麻煩了,而且容易寫錯(cuò)。早在 C# 3.5 的時(shí)候,就有了更好的寫法,使用 Lazy<T>。

示例代碼:

public class LazySingleton
{
   private static readonly Lazy<LazySingleton> _instance =
       new Lazy<LazySingleton>(() => new LazySingleton());

   public static LazySingleton Instance
   {
       get { return _instance.Value; }
   }
}

調(diào)用示例:

public class Program
{
   public static void Main()
   {
       var instance = LazySingleton.Instance;
   }
}

使用 Lazy<T> 可以使對(duì)象的實(shí)例化延遲到第一次被調(diào)用的時(shí)候執(zhí)行,通過訪問它的 Value 屬性來創(chuàng)建并獲取實(shí)例,并且讀取一個(gè) Lazy<T> 實(shí)例的 Value 屬性只會(huì)執(zhí)行一次實(shí)例化代碼,確保了線程安全。

到此,相信大家對(duì)“C#中單例模式的實(shí)現(xiàn)代碼”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI