यहां उदाहरण में एक स्टेटिक Demo क्लास बनाई गई है और एक स्टेटिक वेरिएबल count घोषित किया गया है।
यहां count वेरिएबल को ग्लोबल वेरिएबल के रूप में माना जाता है। इसलिए यह उदाहरण में बढ़ता रहता है, क्योंकि क्लास का केवल एक इंस्टेंस बनाया जाता है।
उदाहरण
static class Demo{
public static int count;
static Demo(){
System.Console.WriteLine("Static Constructor called");
}
}
class Program{
static void Main(){
Demo.count++;
Demo.count++;
System.Console.WriteLine(Demo.count);
Console.ReadKey();
}
}
आउटपुट
Static Constructor called 2