您的位置:首页 > 编程语言 > C#

c# language note

2016-05-08 20:42 253 查看

Static Class

A static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type.

The following list provides the main features of a static class:

Contains only static members.

Cannot be instantiated.

Is sealed.

Cannot contain Instance Constructors.

Members

The kinds of a class may contain: Fields, Constants, Properties, Methods, Events, Operators, Indexers, Constructors, Destructors, Nested Types.

Instance Constructors

Instance constructors are used to create and initialize any instance member variables when you use the new expression to create an object of a class. To initialize a static class, or static variables in a non-static class, you must define a static constructor.

Private Constructors

If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

Static Constructors

Static constructor is called at most one time, before any instance constructor is invoked or member is accessed. Static constructors have the following properties:

A static constructor does not take access modifiers or have parameters.

A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

A static constructor cannot be called directly.

The user has no control on when the static constructor is executed in the program.

A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.

Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.

C# Singleton Pattern

In most cases, static initialization is the preferred approach for implementing a Singleton in .NET.

public sealed class SingletonStaticInitialization
{
private static readonly SingletonStaticInitialization _instance = new SingletonStaticInitialization();
private SingletonStaticInitialization() { }
public static SingletonStaticInitialization Instance
{
get { return _instance; }
}
}


Upgraded version as below

public sealed class SingletonFullyLazyInstantiation
{
private SingletonFullyLazyInstantiation() { }
public static SingletonFullyLazyInstantiation Instance { get { return Nested.instance; } }

private class Nested
{
//Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
static Nested() { }
internal static readonly SingletonFullyLazyInstantiation instance = new SingletonFullyLazyInstantiation();
}
}


Difference between Singleton Pattern and Static Class

A singleton allows access to a single created instance - that instance
(or rather, a reference to that instance) can be passed as a parameter
to other methods, and treated as a normal object.

A static class allows only static methods.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: