C#
.Net Framework targeting pack
目标包用于当你安装了高版本sdk需要定位到低版本的情况下需要安装的一种包。这种包可以在需要的情况下让你可以选择低版本的编译目标(framework) 而免于安装低版本的sdk。
Access Modifiers
- protected internal: Access is limited to the current assembly or types derived from the containing class. 当前程序集或者任何程序集的子类,相当于protected和internal的并集。
- private protected: Access is limited to the containing class or types derived from the containing class within the current assembly.当前类或者当前程序集的子类,相当于protected和internal的交集。
单例模式Singleton
//单线程单例模式,不适用多线程
public class Singleton
{
private Sington() { }
private static Singleton _Singleton = null;
public static Singleton CreateInstance()
{
if (_Singleton == null)
{
Console.WriteLine("被创建");
_Singleton = new Singleton();
}
return _Singleton;
}
}
//多线程单例模式
public class Singleton
{
private Sington() { }
private static Singleton _Singleton = null;
private static object Singleton_Lock = new object(); //锁同步
public static Singleton CreateInstance()
{
if (_Singleton == null) //双if +lock
{
lock (Singleton_Lock)
{
Console.WriteLine("路过");
if (_Singleton == null)
{
Console.WriteLine("被创建");
_Singleton = new Singleton();
}
}
}
return _Singleton;
}
}
//利用静态变量实现单例模式
public sealed class Singleton
{
private Singleton() { }
private static readonly Singleton singleInstance = new Singleton();
public static Singleton GetInstance
{
get
{
return singleInstance;
}
}
}
//利用静态构造函数实现单例模式
public class SingletonSecond
{
private static SingletonSecond _SingletonSecond = null;
static SingletonSecond()
{
_SingletonSecond = new SingletonSecond();
}
public static SingletonSecond CreateInstance()
{
return _SingletonSecond;
}
}
static
- 静态类不能实例化
- 静态构造函数可以用于静态类,也可用于非静态类,静态构造函数自动执行,并且只执行一次。
构造函数私有
将构造函数申明为私有的,则不能通过new运算符在外部代码中实例化。
- 永远不会实例化,因为它仅用作某些静态成员的容器
- 希望类只能通过调用某个静态方法来实例化