简体   繁体   中英

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.

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.

Static members are generally thread safe apart from some special cases of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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