• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

Singleton example

Hi you experts.

Can you give an example of a singleton class?
I need an example in which a property of a class is filled(data are passed to the class).


Thank you all in advance
0
adktd
Asked:
adktd
  • 4
1 Solution
 
AGBrownCommented:
Depending on your use for the singleton your declaration structure will change, but for a thoroughly thread-safe singleton, you can do:

      public class SingletonUser
      {
            public void SetSingletonPropertyA(string theValue)
            {
                  MySingletonClass.Instance.PropertyA = theValue;
            }
      }
      public sealed class MySingletonClass
      {
            #region Singleton implementation
            /// <summary>
            ///            Thread-safe singleton implementation, bypassing the need for lazy implementation and double-lock check.
            /// </summary>
            public static readonly MySingletonClass Instance = new MySingletonClass();
            /// <summary>
            ///            Private so that only this class can declare itself
            /// </summary>
            private MySingletonClass() {}
            #endregion Singleton implementation
            private string _PropertyA;
            public string PropertyA
            {
                  get
                  {
                        return this._PropertyA;
                  }
                  set
                  {
                        this._PropertyA = value;
                  }
            }
      }

There is a very good MSDN article on singletons here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/singletondespatt.asp). It is a bit old, but I don't know of any more recent articles.

Andy
0
 
adktdAuthor Commented:
Thank you so much.I'll try it and i'll let you know how it goes.
0
 
AGBrownCommented:
No problem

Andy
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
bsmuehmerCommented:
I only want to add another MSDN article:
       http://msdn.microsoft.com/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp

It is like the other one, but IMHO better shows the different aspects of how to implement it with C#/.Net.
0
 
AGBrownCommented:
The interesting thing about that article is that it mentions only three options for creating your singleton:
1) Non-thread safe, lazy initialisation
2) Non-thread safe using readonly.
3) Double-check locking lazy initialisation and volatile mark.

The other article adds the fourth method that I posted above which is (was at the time of writing? maybe is now possible?) not possible in Java; a .NET specific implementation almost exactly the same as the 3rd, but a lot simpler. This is still lazily initialised; is thread-safe; and still gets around the problem wherein some compilers reorder the lazy initialisation, and reintroduce the thread-safety issues that the double-lock check tries to get rid of. All of it is done by relying on the .NET framework behaviours.

Its also a lot, lot simpler to write the third method than either of the first two.

I don't yet know of any reason not to use this implementation in favour of any of the first 3. I would love to be proved wrong though!

Andy
0
 
AGBrownCommented:
adktd,

Did you have any luck with this?

Andy
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now