#256 – Using Static Fields
February 28, 2011 Leave a comment
Let’s say that we have both instance and static fields defined in a class:
public string Name; public static string Creed;
This allows us read and write the Name property for each instance of the Dog class that we create.
Dog kirby = new Dog(); kirby.Name = "Kirby"; Dog jack = new Dog(); jack.Name = "Jack";
Notice that each instance of a Dog has its own copy of the Name field, since Name is an instance field.
We can also read/write the static Creed field, using the class name to qualify the field, rather than an instance variable.
Dog.Creed = "Man's best friend";
There is only one copy of the Creed field, since Creed is a static field.