当前位置:网站首页>C# 单例模式
C# 单例模式
2022-08-10 01:18:00 【ou.cs】
using System;
namespace 单例模式
{
internal class Program
{
public abstract class Singletion<T> where T : new()
{
private static T instance;
private static readonly object obj = new object();
public static T Instance
{
get
{
if (instance == null)
{
lock (obj)
{
if (instance == null)
instance = new T();
}
}
return instance;
}
}
}
static void Main(string[] args)
{
//Test.Instance.val = 444;
//Console.WriteLine(Test.Instance.val);
Console.WriteLine(Singletion<Test>.Instance.val);
//Console.WriteLine(Test.Instance.Equals(Singletion<Test>.Instance));
}
public class Test
{
public int val = 123;
public string str = "123";
}
}
}
public abstract class Singletion<T> where T : new()
{
private static T? instance= default;
private static readonly object obj = new object();
public static T Instance
{
get
{
if (instance == null)
{
lock (obj)
{
if (instance == null)
instance = new T();
}
}
return instance;
}
}
}
边栏推荐
- 翻译工具-翻译工具下载批量自动一键翻译免费
- 《GB39732-2020》PDF下载
- [LeetCode] Find the sum of the numbers from the root node to the leaf node
- 商业模式及其 SubDAO 深入研究
- Sikuli's Automated Testing Technology Based on Pattern Recognition
- Solve the problem of sed replacement text containing special characters such as "/" and "#"
- mstsc/Mstsc (Microsoft terminal services client)远程桌面连接
- .Net interview experience summary
- DP 优化方法合集
- Shader Graph学习各种特效案例
猜你喜欢
随机推荐
进程管理和任务管理
Unity editor extension interface uses List
解决sed替换文本,里面含有“/“、“#”等特殊字符的问题
mstsc/Mstsc (Microsoft terminal services client)远程桌面连接
五种定位——粘性定位
【无标题】
信息化和数字化的核心差异
C# 四舍五入 MidpointRounding.AwayFromZero
按钮倒计时提醒
彩色袜子题
Interdepartmental Communication Skills
RedHat红帽RHEL7安装与使用,VMware Workstation16 Pro虚拟机的安装与使用
unity 报错 Unsafe code may only appear if compiling with /unsafe. Enable “Allow ‘unsafe‘ code“ in Pla
【干货】集成学习原理总结
谷歌翻译器-谷歌翻译器软件批量自动翻译
22.括号生成
【kali-密码攻击】(5.2.1)密码分析:Hash Identifier(哈希识别)
Initial attempt at UI traversal
Entity FrameWork Core教程,从基础应用到原理实战
web开发概述