O'Reilly logo

Pro C# 2010 and the .NET 4 Platform, Fifth Edition by Andrew Troelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.4. Understanding the static Keyword

A C# class may define any number of static members using the static keyword. When you do so, the member in question must be invoked directly from the class level, rather than from an object reference. To illustrate the distinction, consider your good friend System.Console. As you have seen, you do not invoke the WriteLine() method from the object level:

// Error!  WriteLine() is not an object level method!
Console c = new Console();
c.WriteLine("I can't be printed...");

but instead simply prefix the class name to the static WriteLine() member:

// Correct!  WriteLine() is a static method.
Console.WriteLine("Thanks...");

Simply put, static members are items that are deemed (by the class designer) to be so commonplace ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required