简体   繁体   English

在类中使用私有静态变量是否合适?

[英]Is it good to have private static variables in a class?

In a ASP.net web application there are Private static variables which either holds the values from DB tables field or NULL. 在ASP.net Web应用程序中,有私有静态变量,它们保存来自DB表字段或NULL的值。

private static decimal? X = null;

if (tdataItems.Y != null)
{
    X = tbldataItems.Y;
}

the scope is limited to class and its not shared across pages. 范围仅限于类,而不是跨页面共享。

is it good to have such private static variables? 有这样的私有静态变量是好的吗? i was going through the thread safety issue of static variables so bit confused. 我正在经历静态变量的线程安全问题,所以有点困惑。

any help or redirection will be highly appreciated! 任何帮助或重定向将受到高度赞赏!

Thanks! 谢谢!

By default C# class members are private so you can get rid of this access modifier. 默认情况下,C#类成员是私有的,因此您可以摆脱此访问修饰符。

Static members are generally thread safe apart from some special cases of Remoting. 除了Remoting的一些特殊情况之外,静态成员通常是线程安全的。

But you should be aware while using static members as their scope is not under any object but is completely determined by the CLR and over-usage can compromise efficiency. 但是在使用静态成员时应该注意,因为它们的范围不在任何对象之下,但完全由CLR决定,过度使用可能会影响效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM