source/Repid.Utility/Helper/SingletonHelper.cs

(完整代码) 1KB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Repid.Utility.Helper
{
    /// <summary>
    /// 单例工具
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    public static class SingletonHelper<T> where T : class, new()
    {
        /// <summary>
        /// 同步对象
        /// </summary>
        private static readonly object sync = new object();

        /// <summary>
        /// 唯一实例
        /// </summary>
        private static T singleton;

        /// <summary>
        /// 实例对象
        /// </summary>
        public static T Instance
        {
            get
            {
                if (singleton == null)
                {
                    lock (sync)
                    {
                        if (singleton == null)
                        {
                            singleton = new T();
                        }
                    }
                }

                return singleton;
            }
        }
    }
}

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。