永远到底有多远
看不知名的电影,
女的问:“你敢不敢再神的面前发誓永远爱我”
男的犹豫了下,女的说:想这么久,一定是不会。
于是男的微笑了下说:“你再问一遍。”因为他以为他这次会立刻答应。
女的重复了一遍。可男的没有回答,他以为说出永远很轻松,其实永远很沉重,他没有说出口…
老套的电影情节。
永远到底有多远,我的脑海里浮现出众多画面,笔直伸向远方变得越来越细的公路,电子时钟上闪动的黑点,无尽的天空,终究我无法把握永远。永远总是使我颇为感慨。记得曾经的女友和现在的女友都曾问过我关于是否会永远爱她之类的问题,我总是敷衍或是沉默,因为我不知道永远到底有多远。不晓得人们干嘛会问这么幼稚的问题,即便是得到了一个肯定的答案,那么他或者她就一定会坚守到永远吗.分手的一如既往的分手,移情别恋的义无反顾的移情别恋,出轨的照旧出轨,我无法把握未来,所以我无法百分之百诚心诚意的向谁承诺永远。也许问出类似永远这样问题的人也未必能够完全把握永远的含义。
即便如此,人们还是会问会不会永远爱我,也会出于各种目的承诺永远。我们要的只是一种语言上的慰藉,所以当你的恋人问道你永远的问题时,你应当适可而止的敷衍一下,当然对于恋人永远的承诺也不要过于认真,因为没有人知道未来是什么,没有人知道永远有多远。
相信爱情,不相信永远。永远只会仍彼此的心感到沉重。自由的爱情,自由的心,没有永远的永远….
Effective C# 原则13:用静态构造函数初始化类的静态成员
Item 13: Initialize Static Class Members with Static Constructors
(译注:initializer在上文中译为了“初始化器”,实在不好听,本文中全部改译为:“预置方法”)
你应该知道,在一个类型的任何实例初始化以前,你应该初始化它的静态成员变量。在里C#你可以使用静态的预置方法和静态构造函数来实现这个目的。一个类的静态构造函数是一个与众不同的,它在所有的方法,变量或者属性访问前被执行。你可以用这个函数来初始化静态成员变量,强制使用单件模式,或者实现其它任何在类型的实例可用前应该完成的工作。你不能用任何的实例构造函数,其它特殊的私有函数, 或者任何其它习惯方法来初始化一个 变量(译注:编译器就不让你这样做,所以你不用担心这样的问题)。
和实例的预置方法一样,你可以把静态的预置方法做为静态构造函数可替代的选择。如果须要简单的分配一个静态成员,就直接使用初始化语法。当你有更复杂的逻辑来初始化静态成员变量时,就创建一个静态构造函数:
public class MySingleton
…{
private static readonly MySingleton _theOneAndOnly =
new MySingleton( );
public static MySingleton TheOnly
…{
get
…{
return _theOneAndOnly;
}
}
private MySingleton( )
…{
}
// remainder elided
}
可以用下面的方法简单的实现单件模式,实际上你在初始化一个单件模式时可能有更复杂的逻辑:
public class MySingleton
…{
private static readonly MySingleton _theOneAndOnly;
static MySingleton( )
…{
_theOneAndOnly = new MySingleton( );
}
public static MySingleton TheOnly
…{
get
…{
return _theOneAndOnly;
}
}
private MySingleton( )
…{
}
// remainder elided
}

同样,和实例的预置方法一样,静态的预置方法在静态的构造函数调用前执行。并且,你的静态预置方法在基类的静态构造函数执行前被执行。
当应用程序第一次装载你的数据类型时,CLR自动调用静态构造函数。你只能定义一个静态构造函数,并且不能有参数。因为静态构造函数是CLR调用的,你必须十分注意异常的产生。如果在静态构造函数里产生了异常,CLR将会直接终止你的应用程序。正因为异常,静态构造函数常常代替静态预置方法。如果你使用静态预置方法,你自己不能捕获异常。做为一个静态的构造,你可以这样(参见原则45):
static MySingleton( )
…{
try …{
_theOneAndOnly = new MySingleton( );
} catch
…{
// Attempt recovery here.
}
}
静态预置方法和静态构造函数为你的类提供了最清爽的方法来初始化静态成员。与其它语言不同,它们被添加到C#语言中,是初始化静态成员的两个不同的特殊位置。
===================
Item 13: Initialize Static Class Members with Static Constructors
You know that you should initialize static member variables in a type before you create any instances of that type. C# lets you use static initializers and a static constructor for this purpose. A static constructor is a special function that executes before any other methods, variables, or properties defined in that class are accessed. You use this function to initialize static variables, enforce the singleton pattern, or perform any other necessary work before a class is usable. You should not use your instance constructors, some special private function, or any other idiom to initialize static variables.
As with instance initialization, you can use the initializer syntax as an alternative to the static constructor. If you simply need to allocate a static member, use the initializer syntax. When you have more complicated logic to initialize static member variables, create a static constructor.
Implementing the singleton pattern in C# is the most frequent use of a static constructor. Make your instance constructor private, and add an initializer:
public class MySingleton
{
private static readonly MySingleton _theOneAndOnly =
new MySingleton( );
public static MySingleton TheOnly
{
get
{
return _theOneAndOnly;
}
}
private MySingleton( )
{
}
// remainder elided
}
The singleton pattern can just as easily be written this way, in case you have more complicated logic to initialize the singleton:
public class MySingleton
{
private static readonly MySingleton _theOneAndOnly;
static MySingleton( )
{
_theOneAndOnly = new MySingleton( );
}
public static MySingleton TheOnly
{
get
{
return _theOneAndOnly;
}
}
private MySingleton( )
{
}
// remainder elided
}
As with instance initializers, the static initializers are called before any static constructors are called. And, yes, your static initializers execute before the base class’s static constructor.
The CLR calls your static constructor automatically when your type is first loaded into an application space. You can define only one static constructor, and it must not take any arguments. Because static constructors are called by the CLR, you must be careful about exceptions generated in them. If you let an exception escape a static constructor, the CLR will terminate your program. Exceptions are the most common reason to use the static constructor instead of static initializers. If you use static initializers, you cannot catch the exceptions yourself. With a static constructor, you can (see Item 45):
static MySingleton( )
{
try {
_theOneAndOnly = new MySingleton( );
} catch
{
// Attempt recovery here.
}
}
Static initializers and static constructors provide the cleanest, clearest way to initialize static members of your class. They are easy to read and easy to get correct. They were added to the language to specifically address the difficulties involved with initializing static members in other languages.
