Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Tuesday, 7 March 2017

Singleton In C# With Example

 No comments   


Introduction
In this post i will explain about what is Singleton in c# and why we need to use this .


Singleton Class

As its name suggest Singleton is used for only a single object creation in OOP. This is very powerful Technic for our programming . 
In many situations we came across where we need to create a single object of our class and use this object in whole application every-time Just like in login system and also when interacting with database every time.
Example:


class TestSingleton
    {
        private TestSingleton()
        {

        }

        public static TestSingleton objSingleton = null;

        public static TestSingleton NewSingletonObject()
        {
            if(objSingleton == null)
            {
                objSingleton = new TestSingleton();
            }
            return objSingleton;
        }
    }

Create object of TestSngleton ............... :-


class Program
   {
       static void Main(string[] args)
       {
           TestSingleton.NewSingletonObject();
       }
   }


Explanation:
In the above example first i create a class name TestSingleton and after this define a private constructor for this class, this private constructor restrict user from object creation.
               After this we need to declare a static variable for object counts and a static method for object creation of this TestSingleton.
After first object creation static class type variable incremented by 1 and user never create any other object of this class until this object not destroyed or released

But this is not thread safe . If at the same time more than 1 user hit the same object creation call than it is possible that they all are able to create new instance of this class. Because in our static method no restriction for single user at a time.

Thread Safe Singletone Example


class TestSingleton
   {
       private TestSingleton()
       {
 
       }
 
       public static TestSingleton objSingleton = null;
       private static readonly object lockCurrentObject = new object();
 
       public static TestSingleton NewSingletonObject()
       {
           lock (lockCurrentObject)
           {
               if (objSingleton == null)
               {
 
                   objSingleton = new TestSingleton();
               }
               return objSingleton;
           }
       }
   }

This lockCurrentObject take care of object creation from multiple users or threads. Only one thread will create the instance. After the execution of one thread other thread enters under lock and condition will return false because already object created by 1st thread so it will return false and no new object creation will happen.

Key Points About Singleton Class
  • You did not specify parameters when you create object of Singleton class.
  • Singleton class has a private constructor.
  • Singleton class use static method for object creation.
  • Singleton can implements in different way (None thread Safe, Thread Safe, Double check Locking, Lazy instantiation )




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write