Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 6 MORE ABOUT CLASSES

Class Members

The previous two chapters covered two of the nine types of class members: fields and methods. In this chapter, I’ll introduce more types of class members, and explain their features.

Table 6-1 shows a list of the class member types. Those that have already been introduced are marked with diamonds. Those that are covered in this chapter are marked with a check. Those that will be covered later in the text are marked with empty check boxes.

Table 6-1. Types of Class Members

Data Members

Function Members

 

(Store Data)

(Execute Code)

 

 

 

 

♦ Fields

♦ Methods

Operators

Constants

Properties

Indexers

 

Constructors

Events

 

Destructors

 

 

 

 

Order of Member Modifiers

Previously, you saw that the declarations of fields and methods can include modifiers such as public and private. In this chapter, I’ll discuss a number of additional modifiers. Since many of these modifiers can be used together, the question that arises is, what order do they need to be in?

Class member declaration statements consist of the following: the core declaration, an optional set of modifiers, and an optional set of attributes. The syntax used to describe this structure is the following. The square brackets indicate that the enclosed set of components is optional.

[ attributes ] [ modifiers ] CoreDeclaration

The optional components are the following:

Modifiers

If there are any modifiers, they must be placed before the core declaration.

If there are multiple modifiers, they can be in an order.

Attributes

If there are any attributes, they must be placed before the modifiers and core declaration.

If there are multiple attributes, they can be in any order.

So far, I’ve explained only two modifiers: public and private, and I’ll cover attributes in Chapter 24.

110

CHAPTER 6 MORE ABOUT CLASSES

For example, public and static are both modifiers that can be used together to modify certain declarations. Since they’re both modifiers, they can be placed in either order. The following two lines are semantically equivalent:

public static int MaxVal;

static public int MaxVal;

Figure 6-1 shows the order of the components as applied to the member types shown so far: fields and methods. Notice that the type of the field and the return type of the method are not modifiers— they’re part of the core declaration.

Figure 6-1. The order of attributes, modifiers, and core declarations

111

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]