public class A { public static int X; //1 static A() { X = B.Y + 1; //2 找B.Y时就会执行Y=A.X+1; } } public class B { public static int Y = A.X + 1; static B() { } }
调用A.X B.Y,调用时,几乎同时执行类内部的静态数据成员,结果X=0,Y=1,再调用静态构造函数,X=2
A.X=2,B.Y=1;
public class BaseA { public static MyTest a1 = new MyTest("a1"); public MyTest a2 = new MyTest("a2"); static BaseA() { MyTest a3 = new MyTest("a3"); } public BaseA() { MyTest a4 = new MyTest("a4"); } public virtual void MyFun() { MyTest a5 = new MyTest("a5"); } } public class BaseB : BaseA { public static MyTest b1 = new MyTest("b1"); public MyTest b2 = new MyTest("b2"); static BaseB() { MyTest b3 = new MyTest("b3"); } public BaseB() { MyTest b4 = new MyTest("b4"); } public new void MyFun() { MyTest b5 = new MyTest("b5"); } } static class Program { static void Main() { BaseB baseb = new BaseB(); baseb.MyFun(); Console.Read(); } } public class MyTest { public MyTest(string info) { Console.WriteLine(info); } }
http://www.cnblogs.com/hkncd/archive/2011/06/05/2073404.html
字符串反转(不能用string,考的就是StringBuilder)
public static string Reverse(string str) { if (string.IsNullOrEmpty(str)) { throw new ArgumentException("参数不合法"); } StringBuilder sb = new StringBuilder(str.Length); for (int index = str.Length - 1; index >= 0; index--) { sb.Append(str[index]); } return sb.ToString(); }
public class Hong<T> { public static int count = 0; public Hong() { count++; } public int GetCount() { return count; } } new Hong<string>(); new Hong<string>(); new Hong<string>(); new Hong<bool>(); Hong<bool> h = new Hong<bool>(); var a = h.GetCount();//2