#259 – Static vs. Instance Properties
March 3, 2011 1 Comment
A typical property declared in a class is an instance property, meaning that you have a copy of that property’s value for each instance of the class.
You can also define static properties, which are properties that have a single value for the entire class, regardless of the number of instances of the class that exist.
public class Dog { // An instance property--one copy for each dog public string Name { get; set; } // A static property--one copy for all dogs public static string Creed { get; set; } }
You can read and write a static property even if no instances of the class exist. You use the class’ name to reference a static property.
// Writing an instance property (Name) Dog kirby = new Dog(); kirby.Name = "Kirby"; Dog jack = new Dog(); jack.Name = "Jack"; // Write a static property Dog.Creed = "We are best friends to humans.";