电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

单例类助手(代码)


发布日期:2020/6/20
 

/// <summary>

/// 单例类助手

/// </summary>

public sealed class SingletonHelper<T> where T : new()

{

private static T instance = new T();

private static object locker = new object();

private SingletonHelper() { }

/// <summary>

/// 获取单例

/// </summary>

/// <returns></returns>

public static T GetInstance()

{

if (null == instance)

{

lock (locker)

{

if (null == instance)

{

instance = new T();

}

}

}

return instance;

}

/// <summary>

/// 设置单例

/// </summary>

/// <param name=value></param>

public void SetInstance(T value)

{

instance = value;

}

}

上一篇:如何在Worker thread中更新UI

下一篇:添加一个下拉框到DataGrid