C#中的单例 [英] Singleton in C#
本文介绍了C#中的单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想收集更多的创建单例类的变种。
可以请你提供给我最好的创建方式在C#你的意见。
I would like to collect more variants for create singleton class. Could you please provide to me the best creation way in C# by your opinion.
谢谢。
public sealed class Singleton
{
Singleton _instance = null;
public Singleton Instance
{
get
{
if(_instance == null)
_instance = new Singleton();
return _instance;
}
}
// Default private constructor so only we can instanctiate
private Singleton() { }
// Default private static constructor
private static Singleton() { }
}
推荐答案
我有一个整篇文章,您可能会发现有用
I have an entire article on this which you may find useful.
哦,并且尽量避免使用单例模式,因为它的可测性等的痛苦:)
Oh, and try to avoid using the singleton pattern in general, due to its pain for testability etc :)
这篇关于C#中的单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文