- 类型的各种成员
- 类型的可见性
- 成员的可访问性
- 静态类
- 分部类,结构和接口
- 组件、多态和版本控制
// #define 可以按版本执行不同的代码块#define V1//#define V2a//#define V2b//#define V2c#if !DEBUG#pragma warning disable 414, 67#endifusing System;public sealed class SomeType //P(137){ // 1 // 嵌套类 private class SomeNestedType { } // 2 // 常量 private const Int32 c_SomeConstant = 1; //readonly 只读 // 3 private readonly String m_SomeReadOnlyField = "2"; // 4 //静态可以读/可写字段 private static Int32 s_SomeReadWriteField = 3; // 5 //类型构造器 static SomeType() { } // 6 // 实例构造器 public SomeType() { } // 7 public SomeType(Int32 x) { } // 8 // 实例方法和静态方法 public static void Main1() { } // 9 public String InstanceMethod() { return null; } // 10 // 实例属性 public Int32 SomeProp { // 11 get { return 0; } // 12 set { } // 13 } //实例有参属性(索引器) public Int32 this[String s] { // 14 get { return 0; } // 15 set { } // 16 } // 实例事件 public event EventHandler SomeEvent; // 17}internal static class OverridingAccessibility{ class Base { //基类虚方法 protected virtual void M() { } } class Derived1 : Base { //重写基类虚方法 protected override void M() { } } class Derived2 : Base { protected override void M() { } public static void Main1() { } }}//静态类 staitc关键字 永远不需要实例化internal static class DifferentCalls //P(146){ public static void Go() { Console.WriteLine(); //调用一个静态方法 Object o = new Object(); o.GetHashCode(); // 调用虚实例方法 o.GetType(); // 调用非虚实例方法 }}#region Versioning Components With Virtual Methodspublic sealed class VersioningComponentsWithVirtualMethods{ public static void Main() { CompanyB.BetterPhone phone = new CompanyB.BetterPhone(); phone.Dial(); }}///#if V1namespace CompanyA//P(150){ public class Phone { public void Dial() { Console.WriteLine("Phone.Dial"); // 这里执行拨号操作 } }}namespace CompanyB{ public class BetterPhone : CompanyA.Phone//P(151) { // 加了new 关键字 新的Dail方法变得与Phone的Dial方法无关了 new public void Dial() { Console.WriteLine("BetterPhone.Dial"); EstablishConnection(); base.Dial(); } protected virtual void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); //在这里执行建立连接的操作 } }}#endif///#if V2a || V2b || V2cnamespace CompanyA { public class Phone { public void Dial() { Console.WriteLine("Phone.Dial"); EstablishConnection(); // Do work to dial the phone here. } protected virtual void EstablishConnection() { Console.WriteLine("Phone.EstablishConnection"); // Do work to establish the connection. } }}#endif///#if V2anamespace CompanyB { public class BetterPhone : CompanyA.Phone { //保留关键new,指明该方法与基类型的Dail方法没有关系 new public void Dial() { Console.WriteLine("BetterPhone.Dial"); EstablishConnection(); base.Dial(); } // 为这个方法添加关键new,指明该方法与基类型的 // EstablishConnection 方法没有关系. new protected virtual void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); // 在这里执行建立连接的操作 } }}#endif#if V2bnamespace CompanyB { public class BetterPhone : CompanyA.Phone { //P(153) // 删除Dail方法 (从基类继承Dial) // 移除关键字'new',将关键字Virtual 修改为 override // 指明该方法与基类的EstablishConnection方法的关系 protected override void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); // 在这里执行建立连接的操作 } }}#endif#if V2cnamespace CompanyB { public class BetterPhone : CompanyA.Phone { // Keep 'new' to mark this method as having no // relationship to the base type's Dial method. new public void Dial() { Console.WriteLine("BetterPhone.Dial"); EstablishConnection(); base.Dial(); } // Remove 'new' and change 'virtual' to 'override' to // mark this method as having a relationship to the base protected override void EstablishConnection() { Console.WriteLine("BetterPhone.EstablishConnection"); // Do work to establish the connection. } }}#endif#endregion