How to prove that only one instance of the object is created for static class?

CsharpServer Side ProgrammingProgramming

Here in the example a static Demo class is created and a static variable count is declared.

Here the count variable is treated as a global variable .so it keeps on increasing in the example ,because only one instance of the class is created

Example

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();
   }
}

Output

Static Constuctor called
2
raja
Published on 04-Aug-2020 10:31:44
Advertisements