#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;
        }
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: