constk

Some C# funny static ctor

Mar 4th, 2021
859
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Globalization;
  3.  
  4. namespace CSharpTest
  5. {
  6.     class Account
  7.     {
  8.         public static int staticValue;
  9.         public int instanceValue;
  10.  
  11.         static Account() => staticValue = 10; // is called once then there is any usage of static value or creating first class instance
  12.         public Account(int value) => instanceValue = value;
  13.  
  14.         public static int StaticVaule { get; set; }
  15.         public int InstanceValue { get; set; }
  16.     }
  17.  
  18.     class Program
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             CultureInfo.CurrentCulture = new CultureInfo("en-US", false);
  23.  
  24.             Console.WriteLine(Account.staticValue); // 10
  25.             Account.staticValue = 14;
  26.             Console.WriteLine(Account.staticValue); // 14
  27.  
  28.             Account acc = new Account(100);
  29.             Console.WriteLine(Account.staticValue); // 14
  30.  
  31.             Console.ReadKey();
  32.         }
  33.     }
  34. }
  35.  
RAW Paste Data