I created a static variable in a class and incrementing in the constructor, thinking that whenever I created an instance to the class, the static variable will be reset to 0. To my surprise, the first time when I created the object to the class, static variable(Counter) is incremented to 1. The second time when I created the object to the class, the static variable is retaining the incremented value(1). Is this the behavior of static?
class Singleton
{
private static int counter = 0;
Public Singleton()
{
counter++;
Console.WriteLine("Counter-" + counter);
}
}
static void Main(string[] args)
{
Singleton objSingleton1 = new Singleton();
objSingleton1.getMessage("Hi This is my first message!");
Singleton objSingleton2 = new Singleton();
objSingleton2.getMessage("Hi This is my second message!");
}