#257 – Private Static Fields

A class can define not just public static fields, but also private static fields.  Because the field is static, there is just one copy of the associated data stored–regardless of the number of instances of the class.  And because the field is private, it’s not visible to code outside of the class.

In the example below, the Dog class defines two private static variables, used for tracking the last dog who barked.

        // Static fields
        private static DateTime LastTimeSomebodyBarked;
        private static string LastDogToBark;

        // Instance method
        public void Bark()
            Console.WriteLine("{0}: Woof", Name);
            LastTimeSomebodyBarked = DateTime.Now;
            LastDogToBark = Name;