本文介绍了你能让我知道私人静态建设者的作用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 刚刚遇到一个带私有静态构造函数的调度程序代码 Hello all,Just came across a scheduler code having a class with private static constructorinternal 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 appreciatedThanks!What I have tried:Just came across a scheduler code having a class with private static constructor 推荐答案 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. 这篇关于你能让我知道私人静态建设者的作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 11:10