Creating Fields and Using Initializers

The first type of class member we'll take a look at is the field, also called a data member. A field is just a class-level variable, outside any method. If you make your field public, it's accessible using an object of your class; for example, take a look at the Messager class here, which has a field named Message that holds the message that a method named DisplayMessage will display:

class Messager
  public string message;

  public void DisplayMessage()


Although this example shows how to make a field public, it's usually not good programming practice to give external code direct access to the data in your objects. Instead, it's better to ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.