Search⌘ K

Static Members

Explore how static members differ from instance members in C#, enabling shared data across all instances. Understand static methods, constructors, and classes, including their usage and restrictions, to build efficient and organized code with C#.

Static vs. instance members

We’ve used the static keyword regularly throughout this course, but haven’t really talked about what that means.

Consider the following class:

class Person
{
	public int Age { get; set; }
	public string Name { get; set; }
}

To use this class, we have to create its instance:

var person1 = new Person();
person1.Age = 41;
person1.Name = "John Doe";

var person2 = new Person();
person2.Age = 38;
person2.Age = "Jim Terry";

We now have two instances of the Person class, and each instance has its own copy of the Age and Name properties.

In contrast to instance members that are bound to concrete instances of the class, static members are bound to the class definition ...