code_junkie

Order of static constructors/initializers in C#

Nov 11th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. static private List<int> a = new List<int>() { 0 };
  2. static private List<int> b = new List<int>() { a[0] };
  3.  
  4. static private List<int> a = new List<int>() { b[0] };
  5. static private List<int> b = new List<int>() { a[0] };
  6.  
  7. static private List<int> a = new List<int>() { 1 };
  8. static private List<int> b = new List<int>() { a[0] };
  9.  
  10. static private List<int> a = new List<int>() { b[0] };
  11. static private List<int> b = new List<int>() { 1 };
  12.  
  13. public class A {
  14. public static List<int> a = new List<int>() { B.b[0] };
  15. }
  16. public class B {
  17. public static List<int> b = new List<int>() { A.a[0] };
  18. }
  19.  
  20. try { Console.WriteLine(B.b); } catch (Exception e) { Console.WriteLine(e.InnerException.Message.); }
  21. try { Console.WriteLine(A.a); } catch (Exception e) { Console.WriteLine(e.InnerException.Message); }
  22. try { Console.WriteLine(B.b); } catch (Exception e) { Console.WriteLine(e.InnerException.Message); }
  23.  
  24. The type initializer for 'A' threw an exception.
  25. Object reference not set to an instance of an object.
  26. The type initializer for 'A' threw an exception.
  27.  
  28. static private List<int> a;
  29. static private List<int> b;
  30.  
  31. static SomeClass()
  32. {
  33. a = new List<int>() { 0 };
  34. b = new List<int>() { a[0] };
  35. }
  36.  
  37. static private List<int> a = new List<int>() { 0 };
  38. static private List<int> b = new List<int>() { a[0] };
  39.  
  40. static private List<int> b = new List<int>() { a[0] };
  41. static private List<int> a = new List<int>() { 0 };
  42.  
  43. static MyClass()
  44. {
  45. a = new List<int>() { 0 };
  46. b = new List<int>() { a[0] };
  47. }
Add Comment
Please, Sign In to add comment