What does `static` mean in c#?
Asked Answered
M

5

71

I am really confused with the real meaning of the static keyword in C#. I have gone through different articles on internet but none of them are really helping me to understand it's meaning and other sources are not trusted. I know Stack Overflow has some brilliant minds who can help me understand the real meaning of static like

  • When they get initialized.
  • static methods, properties, classes and constructors
  • Static vs readonly vs constant
Maxentia answered 23/2, 2012 at 9:55 Comment(3)
codeproject.com/Articles/15269/Static-Keyword-DemystifiedAvionics
Is this a homework questions?Fredrickafredrickson
Did you read the MSDN documentation on static? What there do you not understand?Bypass
D
108

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.

Dissimulate answered 23/2, 2012 at 9:59 Comment(4)
Your link to blog post is broken, according to this post https://mcmap.net/q/160938/-usage-of-appdomain-in-c appdomain share heap, is it true that static class always has 1 instance in heap that they share across appdomains, is that true ?Austen
What if my strongly typed dataAccess class has all static methods (which are creating new instances of table-adapter), would it be considered bad practice ?Austen
@PleaseTeach: Not sure what you mean by strongly typed data access class in this case...Dissimulate
@PleaseTeach: Fixed the link. And no, there isn't 1 instance - there aren't any instances.Dissimulate
C
17

I can recommend this article, it seems pretty descriptive: Static Keyword Demystified

I would also recommend an official c# Programming Guide article which covers the various uses of the static keyword. You can go from there since there are a lot of links to different MSDN articles.: Static Classes and Static Class Members (C# Programming Guide)

Clemmieclemmons answered 23/2, 2012 at 9:59 Comment(0)
Z
11

A little about constant (const) and readonly:

  • constant or const is variable which cannot be modified,and which value is known at compile time.
  • readonly is very similar to constant, this cannot be modified either, the difference is that a readonly field can be modified/initialized once in the constructor. After that readonly is the same as constant.

Using examples:

constant: 
const  int a=10; // value cannot be modified, value is known at compile time

But what to do when we want constant field whos value is not known at compile time?

e.g const PersonClass a=new PersonClass("name"); // error

The answer is a readonly field:

readonly:
readonly PersonClass a=new PersonClass("name"); // all correct
Zambrano answered 23/2, 2012 at 10:13 Comment(0)
N
8

From documentation:

The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration

Static members are intializeed on first access to the class and are executed in textual order.

Static methods, properties are parts of the class and not instance.

Static has nothing to do with readonly or constant. Static is a way like a member acessed, readonly and constant is way like a member stored/managed.

Nitroglycerin answered 23/2, 2012 at 10:1 Comment(0)
C
0

The static keyword is used in C# to indicate that a member belongs to the type itself rather than to a specific object. This means that only one copy of the member exists, regardless of how many objects are created from the type. Static members are also often used to create utility or helper methods that don't need access to an object's data or behavior.

Castilian answered 5/3, 2023 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.