Home > Articles

  • Print
  • + Share This
This chapter is from the book

Creating Static Members

So far, the fields, methods, and properties we've been creating have all been members of objects. You can also create fields, methods, and properties that you use with classes directly, not with objects. These members are called static members, also called class members (not object members), and you use them with the class name, not the name of an object.

As you know, Main is declared static so that C# itself can call it without creating an object from your main class. That's the way it works with all static members—they're intended to be used with the class, not with an object. We'll take a look at creating static members now, starting with static fields.

For C++ Programmers

Here's a big difference between C# and C++: in C#, you cannot access static members of a class using an object of that class, as you can in C++. You must use the class name to access static members, not an object.

  • + Share This
  • 🔖 Save To Your Account