Книга: C# 2008 Programmer

Class Members

Class Members

Variables and functions defined in a class are known as a class's members. The Contact class definition, for instance, has four members that you can access once an object is instantiated:

public class Contact {
 public int ID;
 public string FirstName;
 public string LastName;
 public string Email;
}

Members of a class are classified into two types:

Type Description
Data Members that store the data needed by your object so that they can be used by functions to perform their work. For example, you can store a person's name using the FirstName and LastName members.
Function Code blocks within a class. Function members allow the class to perform its work. For example, a function contained within a class (such as the Contact class) can validate the email of a person (stored in the Email member) to see if it is a valid email address.

Data members can be further grouped into instance members and static members.

Instance Members

By default, all data members are instance members unless they are constants or prefixed with the static keyword (more on this in the next section). The variables defined in the Contact class are instance members:

public int ID;
public string FirstName;
public string LastName;
public string Email;

Instance members can be accessed only through an instance of a class and each instance of the class (object) has its own copy of the data. Consider the following example:

Contact contact1 = new Contact();
contact1.ID = 12;
contact1.FirstName = "Wei-Meng";
contact1.LastName = "Lee";
contact1.Email = "weimenglee@learn2develop.net";
Contact contact2 = new Contact();
contact2.ID = 35;
contact2.FirstName = "Jason";
contact2.LastName = "Will";
contact2.Email = "JasonWill@company.net";

The objects contact1 and contact2 each contain information for a different user. Each object maintains its own copy of the ID, FirstName, LastName, and Email data members.

Static Members

Static data members belong to the class rather than to each instance of the class. You use the static keyword to define them. For example, here the Contact class has a static member named count:

public class Contact {
 public static int count;
 public int ID;
 public string FirstName;
 public string LastName;
 public string Email;
}

The count static member can be used to keep track of the total number of Contact instances, and thus it should not belong to any instances of the Contact class but to the class itself.

To use the count static variable, access it through the Contact class:

Contact.count = 4;
Console.WriteLine(Contact.count);

You cannot access it via an instance of the class, such as contact1:

//---error---
contact1.count = 4;

Constants defined within a class are implicitly static, as the following example shows:

public class Contact {
 public const ushort MAX_EMAIL = 5;
 public static int count;
 public int ID;
 public string FirstName;
 public string LastName;
 public string Email;
}

In this case, you can only access the constant through the class name but not set a value to it:

Console.WriteLine(Contact.MAX_EMAIL);
Contact.MAX_EMAIL = 4; //---error---

Access Modifiers

Access modifiers are keywords that you can add to members of a class to restrict their access. Consider the following definition of the Contact class:

public class Contact {
 public const ushort MAX_EMAIL = 5;
 public static int count;
 public int ID;
 public string FirstName;
 public string LastName;
 private string _Email;
}

Unlike the rest of the data members, the _Email data member has been defined with the private keyword. The public keyword indicates that the data member is visible outside the class, while the private keyword indicates that the data member is only visible within the class.

By convention, you can denote a private variable by beginning its name with the underscore (_) character. This is recommended, but not mandatory.

For example, you can access the FirstName data member through an instance of the Contact class:

//---this is OK---
contact1.FirstName = "Wei-Meng";

But you cannot access the _Email data member outside the class, as the following statement demonstrates:

//---error: _Email is inaccessible---
contact1._Email = "weimenglee@learn2develop.net";

C# has four access modifiers — private, public, protected, and internal.The last two are discussed with inheritance in the next chapter.

If a data member is declared without the public keyword, its scope (or access) is private by default. So, _Email can also be declared like this:

public class Contact {
 public const ushort MAX_EMAIL = 5;
 public static int count;
 public int ID;
 public string FirstName;
 public string LastName;
 string _Email;
}

Оглавление книги


Генерация: 1.877. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз