#609 – Omit the Class Name for Static Members in the Same Class

Normally when you reference static members, you use the class name where they are defined to prefix the name of the member.  For example, if we have a static property in the Dog class called Motto and a static method called ListAllDogs, we’d use these static members from another class as follows:

    public class Program
    {
        static void Main()
        {
            // Reference static members in Dog from outside of Dog class
            Console.WriteLine("Dog motto: {0}", Dog.Motto);
            Dog.ListAllDogs();
        }
    }

However, if we access these static members from code within the Dog class itself, we can omit the class name when referencing the static members.

        // Dog.Bark
        public void Bark()
        {
            Console.WriteLine("{0} says Woof", Name);

            // Reference static members from within Dog class
            Console.WriteLine("Dog motto: {0}", Motto);
            ListAllDogs();
        }
Advertisement

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 )

Twitter picture

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

Facebook photo

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

Connecting to %s

%d bloggers like this: