What does `static` mean in c#?

In short, static effectively means “associated with a type instead of any one instance of the type”. So there’s one set of static variables for a type (within an AppDomain) whether you have 0 instances or a million; you don’t need an instance to access a static member, etc.

The exact point of initialization of static variables depends on whether there’s also a static constructor or not, but very broadly speaking it’s “once, usually before anything significant happens in the class”. (See this blog post for a more detailed description.)

While readonly fields can be either static or instance (i.e. related to the type or related to an instance of the type), const values are always implicitly static (they’re compile-time constants, so it wouldn’t make sense to have one copy per instance).

You may sometimes see static being described as “shared between all instances of a type” – I personally dislike that description, as it suggests that there has to be at least one instance… whereas actually, you don’t need any instances in order to use a static member. I prefer to think of them as entirely separate, rather than being “shared” between instances.

Leave a Comment