Quote:
Using static variable in a program is a "not".
Either the person who told you that doesn't know what they're talking about, or you missed a lot of context in what was said.
static
fields/properties/methods have plenty of valid uses in writing code. You just need to understand what you're doing with them.
For example, a static field can usually* only store a single value for the entire lifetime of the process. If you're writing a web application, for example, then using
static
fields to store user-specific data is a serious error, because the data will be overwritten with data for other users.
But a blanket ban on
static
members based on one example of incorrect behaviour resulting from not understanding them is a serious overreaction.
* "Usually", because you can have "thread-static" fields which store a different value for each thread:
ThreadStaticAttribute Class (System) | Microsoft Learn[^]
Or you can use something like the ThreadLocal<T>
class, where a single static instance of the class stores a different value for each thread:
ThreadLocal<T> Class (System.Threading) | Microsoft Learn[^]
Or you can have static properties which return a different value for each logical "call context" - for example, the HttpContext.Current
property.