none
.NET Singleton Pattern Creation RRS feed

  • Question

  • Hi,

    While creating a singleton Class i came across two type of implementation

    1. Use of lock and double null check pattern

    http://msdn.microsoft.com/en-us/library/ff650316.aspx

    2. Use of Public static readonly pattern in C#.

    http://msdn.microsoft.com/en-us/library/ee817670.aspx#singletondespatt_topic2

    As per second link (please see .NET Singleton Example heading) It says .Net automatically care about multi-threading and one time instance creation.

    So can i just use the 2nd approach, and leave all things to .Net.

    Thanks


    Thanks Kumar Gaurav

    Wednesday, November 28, 2012 6:53 AM

Answers

  • "So can i just use the 2nd approach, and leave all things to .Net."

    In general yes.

    In some cases it may have some unwanted side effects though. For example if you have 2 such static fields in a class then they're both initialized at the same time, when you first access one of them. Even if you do not access the other one it will still be initialized.

    Wednesday, November 28, 2012 9:18 AM
    Moderator