Trong ví dụ này, một lớp Demo tĩnh được tạo và số lượng biến tĩnh được khai báo.
Ở đây, biến count được coi như một biến toàn cục. Vì vậy, nó tiếp tục tăng trong ví dụ, vì chỉ một phiên bản của lớp được tạo
Ví dụ
static class Demo{ public static int count; static Demo(){ System.Console.WriteLine("Static Constuctor called"); } } class Program{ static void Main(){ Demo.count++; Demo.count++; System.Console.WriteLine(Demo.count); Console.ReadKey(); } }
Đầu ra
Static Constuctor called 2