Summary: in this tutorial, you’ll learn about the C# static field and static property.
Introduction to the C# static field
A static field is bound to a class, not an instance of the class. To declare a static field, you use the static
keyword. Consider the following example.
First, define the Person
class:
// Person.cs
class Person
{
public string Name;
public Person(string name)
{
Name = name;
}
}
Code language: PHP (php)
In the Person
class, the Name
is an instance field that is bound to an instance of the Person
class.
This means that each instance of the Person
class has its own Name
field with a separate value.
Second, create two instances of the Person
class:
// Program.cs
var p1 = new Person("John");
var p2 = new Person("Jane");
Console.WriteLine($"p1 Name: {p1.Name}");
Console.WriteLine($"p2 Name: {p2.Name}");
Code language: JavaScript (javascript)
In this picture, p1
and p2
are references that refer to separate Person
‘s objects with their own name fields and values.
Third, add a static field Count
to Person
class and initialize its value to zero:
// Person.cs
class Person
{
public string Name;
public static int Count = 0;
public Person(string name)
{
Name = name;
}
}
Code language: PHP (php)
Unlike an instance field, the compiler creates a separate memory location to store a static field once it encounters the Person
class:
To access the static field Count
inside the Person
class, you reference the field directly:
Count
Since the static field Count
is bound to the Person class, not an instance of the class, you cannot use the this
keyword to reference it. The following statement will result in an error:
this.Count
Code language: JavaScript (javascript)
Fourth, increase the value of the static field Count
by one in the constructor:
// Person.cs
class Person
{
public string Name;
public static int Count = 0;
public Person(string name)
{
Name = name;
Count++;
}
}
Code language: PHP (php)
Fifth, access a static field outside of the class using the ClassName.StaticField
syntax:
// Program.cs
Console.WriteLine(Person.Count); // 0
Code language: JavaScript (javascript)
Finally, create two instances of the Person
class. Each statement implicitly calls the Person
constructor that increases the value of the Count
static field by one:
// Program.cs
Console.WriteLine($"Person count: {Person.Count}"); // 0
var p1 = new Person("John");
var p2 = new Person("Jane");
Console.WriteLine($"Person count: {Person.Count}"); // 2
Code language: JavaScript (javascript)
C# static property
Like a static field, a static property is bound to a class, not any instances of the class. The following example redefines the Person
class with a static property Count
:
class Person
{
public string Name;
public static int Count { get; private set; }
public Person(string name)
{
Name = name;
Count++;
}
}
Code language: PHP (php)
In this example, the Count
property has a public get
accessor and a private set
accessor. This means that you can change the Count
property inside the class and access it from both inside and outside of the class.
Summary
- Use the
static
keyword to define a static field or property in a class. - A static field or property is bound to a class, not a specific instance of the class.