description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | |||
---|---|---|---|---|---|---|---|---|
static modifier - C# Reference |
static modifier |
09/25/2020 |
|
|
5509e215-2183-4da3-bab4-6b7e607a4fdf |
This page covers the static
modifier keyword. The static
keyword is also part of the using static
directive.
Use the static
modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static
modifier can be used to declare static
classes. In classes, interfaces, and structs, you may add the static
modifier to fields, methods, properties, operators, events, and constructors. The static
modifier can't be used with indexers or finalizers. For more information, see Static Classes and Static Class Members.
You can add the static
modifier to a local function. A static local function can't capture local variables or instance state.
You can add the static
modifier to a lambda expression or anonymous method. A static lambda or anonymous method can't capture local variables or instance state.
The following class is declared as static
and contains only static
methods:
[!code-csharpcsrefKeywordsModifiers#18]
A constant or type declaration is implicitly a static
member. A static
member can't be referenced through an instance. Instead, it's referenced through the type name. For example, consider the following class:
[!code-csharpcsrefKeywordsModifiers#19]
To refer to the static
member x
, use the fully qualified name, MyBaseC.MyStruct.x
, unless the member is accessible from the same scope:
Console.WriteLine(MyBaseC.MyStruct.x);
While an instance of a class contains a separate copy of all instance fields of the class, there's only one copy of each static
field.
It isn't possible to use this
to reference static
methods or property accessors.
If the static
keyword is applied to a class, all the members of the class must be static
.
Classes, interfaces, and static
classes may have static
constructors. A static
constructor is called at some point between when the program starts and the class is instantiated.
Note
The static
keyword has more limited uses than in C++. To compare with the C++ keyword, see Storage classes (C++).
To demonstrate static
members, consider a class that represents a company employee. Assume that the class contains a method to count employees and a field to store the number of employees. Both the method and the field don't belong to any one employee instance. Instead, they belong to the class of employees as a whole. They should be declared as static
members of the class.
This example reads the name and ID of a new employee, increments the employee counter by one, and displays the information for the new employee and the new number of employees. This program reads the current number of employees from the keyboard.
[!code-csharpcsrefKeywordsModifiers#20]
This example shows that you can initialize a static
field by using another static
field that is not yet declared. The results will be undefined until you explicitly assign a value to the static
field.
[!code-csharpcsrefKeywordsModifiers#21]
[!INCLUDECSharplangspec]