当前位置:网站首页>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;
}
}
}
边栏推荐
- Premint工具,作为普通人我们需要了解哪些内容?
- Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
- volatile 关键字(修饰符 volatile 告诉编译器,变量的值可能以程序未明确指定的方式被改变)
- 多线程之享元模式和final原理
- Solidity最强对手:MOVE语言及新公链崛起
- 微信账户体系科普:什么是UnionId、OpenId与wxopenid?
- 【wpf】拖拽的简单实现
- 【机器学习】随机森林、AdaBoost、GBDT、XGBoost从零开始理解
- hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
- FILE结构体在stdio.h头文件源码里的详细代码
猜你喜欢

惊掉你下巴,程序员编码竟然可以被 996 指数化

In the 2022 gold, nine, silver and ten work tide, how can I successfully change jobs and get a high salary?

Premint工具,作为普通人我们需要了解哪些内容?

Pyscript,创建一个能执行crud操作的网页应用

夏克-哈特曼波前传感器

用于X射线光学器件的哈特曼波前传感器

元素的盒子模型+标签的尺寸大小和偏移量+获取页面滚动距离

FILE结构体在stdio.h头文件源码里的详细代码

【kali-密码攻击】(5.1.2)密码在线破解:Medusa

ABAP 里文件操作涉及到中文字符集的问题和解决方案
随机推荐
shell指定参数名传参
[Syntax sugar] About the mapping of category strings to category numeric ids
元素的盒子模型+标签的尺寸大小和偏移量+获取页面滚动距离
Golang nil的妙用
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
3438. 数制转换
【wpf】自定义事件总结(Action, EventHandler)
【引用计数器及学习MRC的理由 Objective-C语言】
Visual low-code system practice based on design draft identification
微透镜阵列后光传播的研究
Maya制作赛博朋克机器人模型
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
Solidity最强对手:MOVE语言及新公链崛起
JDBC数据库连接池练习题
Summary of Web Performance Testing Models
FILE结构体在stdio.h头文件源码里的详细代码
UI遍历的初步尝试
sql实战积累
STM32F103驱动HCSR04超声波测距显示