I wonder if there is a mechanism or pattern to allow only one instance of a class in C#. I have heard of the Singleton class, but i don’t know how to use it well.
I wonder if there is a mechanism or pattern to allow only one instance
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using singleton, that is a class which only allows a single instance of itself to be created.
The operation of this pattern is simple and could be reduced to the following:
Hide the constructor of the Singleton class, so that clients may not be instantiated.
To declare the Singleton class private member variable containing the reference to the unique instance that we handle.
Provide in class Singleton a function or property that provides access to the one maintained by the Singleton instance.