public class Test { public const string constStr = "this is a test"; public readonly string readonlyStr = "this can't be modified in runtime time"; public void Method1() { string s1 = constStr; // 在编译的时候,这句话就会被替换成 string s1 = "this is a test"; readonlyStr = "error"; //尝试修改readonlyStr,无法通过编译, //抛出错误:readonly 字段只有在构造函数和初始化的时候才能修改。 } }
class A { public virtual void F() { Console.WriteLine("A.F"); } } class B : A { public override void F() { Console.WriteLine("B.F"); } } class C : B { new public virtual void F() { Console.WriteLine("C.F"); } } class D : C { public override void F() { Console.WriteLine("D.F"); } } class Program2 { static void Main() { D d = new D(); A a = d; B b = d; C c = d; a.F(); b.F(); c.F(); d.F(); } }