Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. public static DataManager()
  2. {
  3. LastInfoID = 1;
  4. }
  5.  
  6. static DataManager() // note no "public"
  7. {
  8. LastInfoID = 1;
  9. }
  10.  
  11. static DataManager()
  12. {
  13. LastInfoID = 1;
  14. }
  15.  
  16. class MyClass
  17. {
  18. static MyClass()
  19. {
  20. // Static constructor
  21. }
  22. }
  23.  
  24. new MyClass();
  25.  
  26. using System;
  27.  
  28. public class Something
  29. {
  30. //
  31. private static DateTime _saticConstructorTime;
  32. private DateTime _instanceConstructorTime;
  33. //
  34. public static DateTime SaticConstructorTime
  35. {
  36. set { _saticConstructorTime = value; }
  37. get { return _saticConstructorTime ; }
  38. }
  39. public DateTime InstanceConstructorTime
  40. {
  41. set { _instanceConstructorTime = value; }
  42. get { return _instanceConstructorTime; }
  43. }
  44. //Set value to static propriety
  45. static Something()
  46. {
  47. SaticConstructorTime = DateTime.Now;
  48. Console.WriteLine("Static constructor has been executed at: {0}",
  49. SaticConstructorTime.ToLongTimeString());
  50. }
  51. //The second constructor started alone at the next instances
  52. public Something(string s)
  53. {
  54. InstanceConstructorTime = DateTime.Now;
  55. Console.WriteLine("New instances: "+ s +"n");
  56. }
  57. public void TimeDisplay(string s)
  58. {
  59. Console.WriteLine("Instance ""+ s + "" has been created at: " + InstanceConstructorTime.ToLongTimeString());
  60. Console.WriteLine("Static constructor has been created at: " + SaticConstructorTime.ToLongTimeString() + "n");
  61. }
  62. }
  63. //
  64. class Client
  65. {
  66. static void Main()
  67. {
  68. Something somethingA = new Something("somethingA");
  69. System.Threading.Thread.Sleep(2000);
  70. Something somethingB = new Something("somethingB");
  71.  
  72. somethingA.TimeDisplay("somethingA");
  73. somethingB.TimeDisplay("somethingB");
  74. System.Console.ReadKey();
  75. }
  76. }
  77. /* output :
  78.  
  79. Static constructor has been executed at: 17:31:28
  80. New instances: somethingA
  81.  
  82. New instances: somethingB
  83.  
  84. Instance "somethingA" has been created at: 17:31:28
  85. Static constructor has been created at: 17:31:28
  86.  
  87. Instance "somethingB" has been created at: 17:31:30
  88. Static constructor has been created at: 17:31:28
  89. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement