Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
A singleton class is only one instance of the class can generated through out the Application. So multiple user can access the Singleton object.
can you provide sample code.

What I have tried:

A singleton class is only one instance of the class can generated through out the Application. So multiple user can access the Singleton object.

C#
    public class loggerclasses
    {

        public static  loggerclasses  instances = null;
        private loggerclasses()
        {
        }

        public static loggerclasses Instances
        {
            get
            {
                if (instances == null)
                {
                    instances = new loggerclasses();
                }

                return instances;
            }

        }
}


How to proceed further.
Posted
Updated 9-Jun-17 9:23am
v2
Comments
PIEBALDconsult 8-Jun-17 2:03am    
a) No.
b) Read the expert advice:
http://csharpindepth.com/Articles/General/Singleton.aspx

1 solution

If I understand based on the title of the question, you want a singleton class which can be inherited by other class. Quick solution is to mark the default constructor with the scope - protected, instead of private. In this way, the class is still singleton, but any other class inheriting this class cannot be a singleton.

Further, what you have tried is absolutely correct, except that it needs additional thread safe code as and where required.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900