var book1 = new {

 ISBN = '978-0-470-17661-0',

 Title='Professional Windows Vista Gadgets Programming',

 Author = 'Wei-Meng Lee',

 Publisher='Wrox'

};

So, how are anonymous types useful for your application? Well, they enable you to shape your data from one type to another. You will look into more about this in Chapter 14, which tackles LINQ.

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 = '[email protected]';

Contact contact2 = new Contact();

contact2.ID = 35;

contact2.FirstName = 'Jason';

contact2.LastName = 'Will';

contact2.Email = '[email protected]';

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
Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату