哈哈~~~第一次在CSDN发帖被列为精~~~
解决方案 »
- 视频解析,播放
- 求智能设备中如何实现FTP操作?
- C# listview删除+窗体关闭问题~
- 调用 web service的问题
- DrawLine 线宽不同 画出不同结果?
- 如何得到一个dataset的某个表的行子集并把它构建成另一个dataset对象??
- 下载的C_WinForm实例调试一直说发生生成错误是为什么?
- 怎样做一个操作其它Windows程序的程序
- 怎么保存CSDN这种XML的网页啊,有什么好方法,建议百家争鸣!!!
- 再问DataGrid中删除记录
- 你被NET的程序运行慢惹怒了吗?
- OracleConnection 连接池 观察oracle 确实增加的连接...但是执行还是在同一个连接执行的...
You have accessed an Internet site blocked by your company policy:
http://www.netcsharp.cn/showtopic-512.aspx
This site is categorised as:
uncategorised
公司里只能上CSDN,转贴不能拷过来吗。。
------------------------------------------------
翻译的很好~
2. Derived static constructor
3. Derived instance fields
4. Base static fields
5. Base static constructor
6. Base instance fields
7. Base instance constructor
8. Derived instance constructorSample ProgramFollowing is a sample C# console program that demonstrates an object's initialization order. This program creates a "Derived" object that inherits from a "Base" object, both of which contain static and instance constructors and fields. Two fields "Field1″ and "Field2″ are initialized in their definitions, whereas "Field3″ is initialized in the constructor. Also included is a virtual method that demonstrates why you should NOT call virtual methods from a constructor. This program writes to the console as each field and constructor is initialized so you can see the initialization order.using System;namespace ObjectInit
{
class Program
{
static void Main( string[] args )
{
Derived d = new Derived();
Console.ReadLine();
}
}
class Base
{
public Base()
{
Console.WriteLine( "Base.Instance.Constructor" );
this.m_Field3 = new Tracker( "Base.Instance.Field3″ );
this.Virtual();
}
static Base()
{
Console.WriteLine( "Base.Static.Constructor" );
}
private Tracker m_Field1 = new Tracker( "Base.Instance.Field1″ );
private Tracker m_Field2 = new Tracker( "Base.Instance.Field2″ );
private Tracker m_Field3;
static private Tracker s_Field1 = new Tracker( "Base.Static.Field1″ );
static private Tracker s_Field2 = new Tracker( "Base.Static.Field2″ );
virtual public void Virtual()
{
Console.WriteLine( "Base.Instance.Virtual" );
}
}
class Derived : Base
{
public Derived()
{
Console.WriteLine( "Derived.Instance.Constructor" );
this.m_Field3 = new Tracker( "Derived.Instance.Field3″ );
}
static Derived()
{
Console.WriteLine( "Derived.Static.Constructor" );
}
private Tracker m_Field1 = new Tracker( "Derived.Instance.Field1″ );
private Tracker m_Field2 = new Tracker( "Derived.Instance.Field2″ );
private Tracker m_Field3;
static private Tracker s_Field1 = new Tracker( "Derived.Static.Field1″ );
static private Tracker s_Field2 = new Tracker( "Derived.Static.Field2″ );
override public void Virtual()
{
Console.WriteLine( "Derived.Instance.Virtual" );
}
}
class Tracker
{
public Tracker( string text )
{
Console.WriteLine( text );
}
}
}
Following is the console output from this sample program: Derived.Static.Field1
Derived.Static.Field2
Derived.Static.Constructor
Derived.Instance.Field1
Derived.Instance.Field2
Base.Static.Field1
Base.Static.Field2
Base.Static.Constructor
Base.Instance.Field1
Base.Instance.Field2
Base.Instance.Constructor
Base.Instance.Field3
Derived.Instance.Virtual
Derived.Instance.Constructor
Derived.Instance.Field3Two-Phase ConstructionNote there is a potential trap with C# object initialization. While initialization starts with derived objects and moves down to the base object, the constructors initialize in reverse order, with the base constructor executing first, then the derived constructor.The problem occurs if you attempt to call a virtual method from the base constructor. Because the derived constructor has not yet executed, the derived object may not be fully initialized when the virtual method executes from the base constructor.As shown in the sample program, when the base constructor calls the virtual method (indicated by "Derived.Instance.Virtual" in the program output), Field3 in the derived object is not initialized because the derived constructor has not yet executed. If the virtual method depends on an initialized Field3, the program will fail.Hence, you should not call a virtual method from an object constructor. Instead, you should perform "two-phase construction" where first you construct the object, then you call the virtual method, as follows:Derived d = new Derived();
d.Virtual();C# Object Initialization TipsSome general rules and tips about C# Object initialization: * Fields then Constructor. Fields are initialized first, then the constructor is executed.
* Static then Instance. Static fields and constructors are initialized the first time a class is accessed. Then the object's instance fields and constructors are initialized.
* Derived then Base. For fields and static constructors, derived objects are initialized before the base object. If class C derives from B derives from A, the fields and static constructors are initialized in order C-B-A.
* Except Instance Constructors. For instance constructors, the base class constructor executes before the derived class constructor. Instance constructors are executed in order A-B-C.
* Don't Assume Field Order. Fields are initialized in the order they are declared in the source file. However, since programmers and tools can reorder field declarations at will, you should not depend on fields being initialized in any particular order.
* Two-Phase for Virtual. Avoid calling virtual methods from a constructor. If virtual methods are required to initialize an object, use two-phase construction where you fully construct an object and then call an initialization method on the constructed object.ShareThisCopyright © 2007-8 Tiwebb Ltd. All rights reserved. This material may not be published, broadcast, rewritten or redistributed without explicit permission from Tiwebb Ltd.
2. Derived static constructor
3. Derived instance fields
4. Base static fields
5. Base static constructor
6. Base instance fields
7. Base instance constructor
8. Derived instance constructorSample ProgramFollowing is a sample C# console program that demonstrates an object's initialization order. This program creates a "Derived" object that inherits from a "Base" object, both of which contain static and instance constructors and fields. Two fields "Field1″ and "Field2″ are initialized in their definitions, whereas "Field3″ is initialized in the constructor. Also included is a virtual method that demonstrates why you should NOT call virtual methods from a constructor. This program writes to the console as each field and constructor is initialized so you can see the initialization order.using System;namespace ObjectInit
{
class Program
{
static void Main( string[] args )
{
Derived d = new Derived();
Console.ReadLine();
}
}
class Base
{
public Base()
{
Console.WriteLine( "Base.Instance.Constructor" );
this.m_Field3 = new Tracker( "Base.Instance.Field3″ );
this.Virtual();
}
static Base()
{
Console.WriteLine( "Base.Static.Constructor" );
}
private Tracker m_Field1 = new Tracker( "Base.Instance.Field1″ );
private Tracker m_Field2 = new Tracker( "Base.Instance.Field2″ );
private Tracker m_Field3;
static private Tracker s_Field1 = new Tracker( "Base.Static.Field1″ );
static private Tracker s_Field2 = new Tracker( "Base.Static.Field2″ );
virtual public void Virtual()
{
Console.WriteLine( "Base.Instance.Virtual" );
}
}
class Derived : Base
{
public Derived()
{
Console.WriteLine( "Derived.Instance.Constructor" );
this.m_Field3 = new Tracker( "Derived.Instance.Field3″ );
}
static Derived()
{
Console.WriteLine( "Derived.Static.Constructor" );
}
private Tracker m_Field1 = new Tracker( "Derived.Instance.Field1″ );
private Tracker m_Field2 = new Tracker( "Derived.Instance.Field2″ );
private Tracker m_Field3;
static private Tracker s_Field1 = new Tracker( "Derived.Static.Field1″ );
static private Tracker s_Field2 = new Tracker( "Derived.Static.Field2″ );
override public void Virtual()
{
Console.WriteLine( "Derived.Instance.Virtual" );
}
}
class Tracker
{
public Tracker( string text )
{
Console.WriteLine( text );
}
}
}
Following is the console output from this sample program: Derived.Static.Field1
Derived.Static.Field2
Derived.Static.Constructor
Derived.Instance.Field1
Derived.Instance.Field2
Base.Static.Field1
Base.Static.Field2
Base.Static.Constructor
Base.Instance.Field1
Base.Instance.Field2
Base.Instance.Constructor
Base.Instance.Field3
Derived.Instance.Virtual
Derived.Instance.Constructor
Derived.Instance.Field3Two-Phase ConstructionNote there is a potential trap with C# object initialization. While initialization starts with derived objects and moves down to the base object, the constructors initialize in reverse order, with the base constructor executing first, then the derived constructor.The problem occurs if you attempt to call a virtual method from the base constructor. Because the derived constructor has not yet executed, the derived object may not be fully initialized when the virtual method executes from the base constructor.As shown in the sample program, when the base constructor calls the virtual method (indicated by "Derived.Instance.Virtual" in the program output), Field3 in the derived object is not initialized because the derived constructor has not yet executed. If the virtual method depends on an initialized Field3, the program will fail.Hence, you should not call a virtual method from an object constructor. Instead, you should perform "two-phase construction" where first you construct the object, then you call the virtual method, as follows:Derived d = new Derived();
d.Virtual();C# Object Initialization TipsSome general rules and tips about C# Object initialization: * Fields then Constructor. Fields are initialized first, then the constructor is executed.
* Static then Instance. Static fields and constructors are initialized the first time a class is accessed. Then the object's instance fields and constructors are initialized.
* Derived then Base. For fields and static constructors, derived objects are initialized before the base object. If class C derives from B derives from A, the fields and static constructors are initialized in order C-B-A.
* Except Instance Constructors. For instance constructors, the base class constructor executes before the derived class constructor. Instance constructors are executed in order A-B-C.
* Don't Assume Field Order. Fields are initialized in the order they are declared in the source file. However, since programmers and tools can reorder field declarations at will, you should not depend on fields being initialized in any particular order.
* Two-Phase for Virtual. Avoid calling virtual methods from a constructor. If virtual methods are required to initialize an object, use two-phase construction where you fully construct an object and then call an initialization method on the constructed object.ShareThisCopyright © 2007-8 Tiwebb Ltd. All rights reserved. This material may not be published, broadcast, rewritten or redistributed without explicit permission from Tiwebb Ltd.
多谢多谢~