Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

Just came across a scheduler code having a class with private static constructor

C#
internal class DBCache
    {
        private static DBCache objDBCache = new DBCache();
        private DataSet dsFieldDetails = new DataSet();

        bool isCached = false;......}


I am not sure why the constructor is declared private and static.

Any ideas! Much appreciated

Thanks!

What I have tried:

C#
Just came across a scheduler code having a class with private static constructor
Posted
Updated 24-Nov-16 23:37pm

A static constructor is used to initialise static members of an object. It is called the once, and once only, when any static method or property is accessed on the type.

However, what you have provided the code there for is not a static constructor. It is a static member that is assigned an instance of an object. Different thing altogether.
 
Share this answer
 
v2
That's not a private static constructor (AFAIK not existing in C#) but a private static member variable holding an instance of the DBCache class).
It could be used, for instance, to implement the Singleton pattern.
 
Share this answer
 
That's not a constructor, it's just a member variable. The fact that it's private means only code inside the DBCache can access it, so code outside the calss can't access it like this;

DBCache.objDBCache

What the line is doing is ensuring that the first time the DBCache type is used the objDBCache variable is set to have a new DBCache object, which means that any time DBCache is used it is guaranteed that objDBCache references an object. The fact that this variable is static also means that there will only ever be one of them so it's like a singleton.

Consider the code below

C#
public class TestClass
{
    private static DateTime dt = DateTime.Now;

    public void DoSomething()
    {
        Console.WriteLine(dt);
    }

    public static void DoSomethingElse()
    {
        Console.WriteLine(dt);
    }
}

static void Main(string[] args)
{
    TestClass tc = new TestClass(); // this is the first time the TestClass type is loaded into this application domain so dt will
                                    // be given a value

    tc.DoSomething(); // this will output dt
    TestClass.DoSomethingElse(); // this will output the same value for dt

    TestClass tc2 = new TestClass(); // we are creating a new instance of TestClass but TestClass already exists in the domain so
                                     // dt will not be updated as that only happens when TestClass is first used

    tc2.DoSomething(); // this will output the same value for dt despite being a different instance
}


So as you can see the use of the private static ensures that variable is assigned once and once only.
 
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