你能让我知道私人静态建设者的作用吗? [英] Could you let me know what private static construtor does?

查看:55
本文介绍了你能让我知道私人静态建设者的作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



刚刚遇到一个带私有静态构造函数的调度程序代码



Hello all,

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

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:

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

推荐答案

静态构造函数用于初始化对象的静态成员。它被称为一次,只有一次,当在类型上访问任何静态方法或属性时。



但是,你提供的代码不是一个静态构造函数。它是一个静态成员,分配了一个对象的实例。完全不同的事情。
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.


这不是私有静态构造函数(AFAIK不存在于 C#)但私有静态成员变量持有 DBCache 类的实例。

例如,它可用于实现 Singleton 模式。
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.


这不是构造函数,它只是一个成员变量。它是私有的这一事实意味着只有DBCache中的代码才能访问它,因此calss之外的代码无法像这样访问它;



DBCache.objDBCache


该行正在做的是确保第一次使用DBCache类型时objDBCache变量被设置为具有新的DBCache对象,这意味着任何时候使用DBCache保证objDBCache引用一个对象。这个变量是静态的这一事实也意味着它们只会有一个,所以它就像一个单身。



考虑下面的代码



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

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.


这篇关于你能让我知道私人静态建设者的作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆