1

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!

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Harshit
  • 149
  • 3
  • 14

1 Answers1

0

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.

Arup Chaudhury
  • 169
  • 3
  • 18
  • thanks @Arup. is it a good approach to have static variables if we want to retain values at page level? – Harshit Dec 16 '16 at 06:38
  • C# is a very explicit language and so are the programmers, which is why you mostly see people explicitly typing the defaults. It improves readability and also shows that you are *intending* it to be `private`. It can also get confusing because the default access level for class is `internal`, not private. – AustinWBryan Jun 25 '18 at 03:57