当前位置:网站首页>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;
}
}
}
边栏推荐
- 浏览器中location详解
- 【SSRF漏洞】实战演示 超详细讲解
- hint: Updates were rejected because the tip of your current branch is behind hint: its remote counte
- 已备案域名用国外服务器会不会掉备案?
- unity编辑器扩展界面使用 List
- 防勒索病毒现状分析
- 力扣每日一题-第51天-744. 寻找比目标字母大的最小字母
- 什么是一网统管?终于有人讲明白了
- 分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
- 这一次,话筒给你:向自由软件之父 Richard M. Stallman 提问啦!
猜你喜欢
【kali-密码攻击】(5.2.1)密码分析:Hash Identifier(哈希识别)
小程序开发的报价为什么有差别?需要多少钱?
基于FTP协议实现文件上传与下载
[Turn] Typora_Markdown_ picture title (caption)
【web渗透】SSRF漏洞超详细讲解
DHCP——动态主机配置协议
Golang nil的妙用
Sikuli 基于图形识别的自动化测试技术
元素的盒子模型+标签的尺寸大小和偏移量+获取页面滚动距离
Chip Information|Semiconductor revenue growth expected to slow to 7%, Bluetooth chip demand still growing steadily
随机推荐
【论文笔记】基于深度学习的机器人抓取虚拟仿真实验教学系统
Unity开发者必备的编辑器技巧
【SSRF漏洞】实战演示 超详细讲解
五种定位——粘性定位
惊掉你下巴,程序员编码竟然可以被 996 指数化
微透镜阵列后光传播的研究
首次在我们的centos上安装MySQL
type-C 边充电边听歌(OTG) PD芯片方案,LDR6028 PD充电加OTG方案
Unity顶点动画
C# rounding MidpointRounding.AwayFromZero
Unity image使用长图后 图片很糊
以太网PHY芯片LAN8720A芯片研究
[论文阅读] Diverse Image-to-Image Translation via Disentangled Representations
UI遍历的初步尝试
20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
【论文粗读】(NeurIPS 2020) SwAV:对比聚类结果的无监督视觉特征学习
你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
[论文阅读] Multimodal Unsupervised Image-to-Image Translation
《GB39732-2020》PDF下载