Skip to main content
deleted 15 characters in body
Source Link

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 utter 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 objSingletonobjSingleton1 = new Singleton.getInstanse;(); objSingletonobjSingleton1.getMessage("Hi This is my first message!"); Singleton objSingleton2 = new Singleton.getInstanse;(); objSingletonobjSingleton2.getMessage("Hi This is my second message!"); } 

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 utter 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 objSingleton = Singleton.getInstanse; objSingleton.getMessage("Hi This is my first message!"); Singleton objSingleton2 = Singleton.getInstanse; objSingleton.getMessage("Hi This is my second message!"); } 

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!"); } 
Post Closed as "Duplicate" by Jon Skeet, juharr, ProgrammingLlama, stuartd c#
Source Link

How static variable can retain incremented value

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 utter 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 objSingleton = Singleton.getInstanse; objSingleton.getMessage("Hi This is my first message!"); Singleton objSingleton2 = Singleton.getInstanse; objSingleton.getMessage("Hi This is my second message!"); }