C# - 类中的静态属性

标签 c#

我有以下代码:

    internal class ModuleLogic
    {
        #region [Private Variables]
        private static ReaderWriterLockSlim _moduleListLock = new ReaderWriterLockSlim();
        private static List<Module> _moduleList;
        #endregion   

        public static void RefreshModuleData()
        {
            _moduleListLock.EnterWriteLock();
            try
            {
                ModuleData.RefreshModuleData(_moduleList);
            }
            finally
            {
                _moduleListLock.ExitWriteLock();
            }
        }
   }

每次访问 RefreshModuleData() 方法时,每次访问都共享两个私有(private)静态变量,我是否正确?

最佳答案

I correct that each time this class is instantiated, the two private static variables are only instantiated once (the first time) and used for each instance

是的,因为它们是静态字段,所以它们只会被实例化。当然,前提是您没有在代码中的任何地方覆盖它们。

关于C# - 类中的静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6966549/

相关文章:

c# - 自定义 map 路线

c# - 为什么 Asp.net core Web API 2.0 返回 Http Error 500

c# - Monotouch (Xamarin.iOS) 内存泄漏

c# - 使用 C#.NET 生成 PDF

c# - 以编程方式为 resx 文件生成 Designer.cs (ResXResourceWriter/ResXResourceReader)

c# - 使用 C# 从 sqlite 3 数据库中检索存储在整数字段中的字符串数据

c# - 在需要条件的上下文中指定的非 bool 类型表达式,位于 'GROUP' 附近

c# - 使用 int 和 uint 的区别以及何时使用

c# - 延迟属性更新,直到更改了一堆属性(即锁定控件)

c# - 了解 C# 中的返回值