Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

longer accessible by any code. Any destructors for inherited classes are invoked at this time as well. A destructor is declared as follows:

~ <identifier>()

{

<statements>

}

where

<identifier> is the name of the class for which the destructor is defined.

<statements> is the block of statements associated with the destructor.

In many, if not most, situations, a destructor is not required. When a Dispose method is required to clean up non-memory resources, a destructor should normally be provided to call the Dispose method in the event a program fails to do so explicitly.

For a Fraction class, a destructor is most likely not required. However, in order to give an example, a destructor for this class might be concocted as follows:

public class Fraction

{

private long _num; private long _den;

. . .

// Destructor (not the best example) ~Fraction()

{

_num = 0;

_den = 1;

}

}

A.2.2 STRUCTURES

A structure is a value type that defines a new data abstraction. Structures are very similar to classes, except that classes are allocated on the heap while structures are allocated in place, either on the stack or within the type that declares them. Structures also cannot be inherited, nor can they inherit from other classes. The default value of a structure instance is the value obtained by setting each value type member to its default value and all reference types to null.

A structure is declared using the struct keyword with the following form:

<modifiers>opt struct <identifier> : <interfaces>opt

{

<struct-members>

}

where

650

APPENDIX A C# PRIMER

<modifiers> is optional, and must be an accessibility level or the keyword new. If unspecified, a structure is assigned the default accessibility level of the containing declarative scope. Multiple complementary modifiers may be specified.

<identifier> is the unique name to assign to the structure.

<interfaces> is optional, and specifies one or more interface types which

this structure supports. If <interfaces> is omitted, then the colon ‘:’ is also omitted.

<struct-members> are the members of the structure. Structures contain the same kinds of members as classes, namely constants, fields, methods, properties, events, indexers, operators, constructors, and nested type declarations. The meaning and purpose of these members is identical to that previously described for classes. One difference is that a default constructor for structures is provided automatically, and cannot be explicitly specified. If not specified, a struct member is assigned the private accessibility level.

Structures are appropriate for short-lived or small objects where local allocation is beneficial. The Fraction class used in examples throughout this appendix might be a good candidate for a structure. Here is an example of a PageRef structure that stores a range of page numbers:

public struct PageRef

{

private int _startPage; private int _endPage;

// Declarations of members to manipulate pages

}

A.2.3 INTERFACES

An interface is a reference type that defines a contract consisting of a set of members. A class or structure supports an interface by specifying the interface in its specification and adhering to the defined contract. This is done by providing implementations of each interface member within the class or structure. An instance of an interface type cannot be explicitly declared, although an instance of a class or structure may be cast to an interface type.

An interface is declared using the interface keyword in the following manner:

<modifiers>opt interface <identifier> : <interfaces>opt

{

<interface-members>

}

where

<modifiers> is optional, and must be an accessibility level or the keyword new. If unspecified, an interface is assigned the default accessibility level of the containing declarative scope. Multiple complementary modifiers may be specified.

TYPES

651

<identifier> is the unique name to assign to the interface. By convention, all interface identifiers begin with a capital I.

<interfaces> is optional, and specifies one or more interface types which must also be supported in order for a class or structure to support this interface. If <interfaces> are omitted, then the colon ‘:’ is also omitted.

<interface-members> are the members required in order to support this interface. The possible members of an interface are methods, properties, events, and indexers. The declarations of these members mimic the declaration shown for classes, except that an implementation is not provided nor is an accessibility level defined. All interface members are considered to be publicly accessible.

Here is an example of an IBookDisplay interface that might be provided to indicate how a book is displayed in a Windows Forms Panel control:

interface IBookDisplay

{

// Interface properties must indicate which accessors to support int ReadingRate

{

get;

set;

}

void BeginDisplay(Panel displayPanel); void NextPage();

void EndDisplay();

Page this[int pageNum];

}

// Class that supports the IBookDisplay interface public class PhotoAlbum : CollectionBase, IBookDisplay

{

//Implementation of IBookDisplay

//interface and other members

}

A.2.4 ENUMERATIONS

An enumeration is a value type that defines a related group of symbolic constants, and is quite similar to enumeration types in C. The default value of an enumeration instance is the value obtained by casting the number zero (0) to the enumeration type. All enumeration types implicitly inherit from the System.Enum class in the

.NET Framework. This class provides a standard set of methods that may be used when manipulating enumerations.

An enumeration is declared using the enum keyword in the following manner:

<modifiers>opt enum <identifier> : <int-type>opt

{

<enum-members>

}

652

APPENDIX A C# PRIMER

Sys-

where

<modifiers> is optional, and must be an accessibility level or the keyword new. If unspecified, an enumeration is assigned the default accessibility level of the containing declarative scope. Multiple complementary modifiers may be specified.

<identifier> is the unique name to assign to the enumeration.

<int-type> is optional, and specifies a built-in integer type to represent the declared enumeration values. This integer type is one of byte, sbyte, short, ushort, int, uint, long, or ulong. If an <int-type> is not specified, the

colon is omitted and the int type is used. Note that the possible values for an enumeration are not limited to its explicitly declared members. Any valid value of the underlying type is a valid value for the enumeration type.

<enum-members> are the members of this enumeration. Each member is written as <identifier> or as <identifier> = <int-value>. Multiple members are separated by commas ‘,’ and each member has an assigned constant integer value. The default assigned value for the first member is zero, and the default value for subsequent members is one greater than the value assigned to the previous member.

Here are a few examples of enumerations:

// Days of week (values 0 to 6)

enum DaysOfWeek1 = { Sun, Mon, Tue, Wed, Thu, Fri, Sat }

// Days of week as unsigned short types (values 1 to 7) enum DaysOfWeek2 : ushort = { Sunday = 1, Monday, Tuesday,

Wednesday, Thursday, Friday, Saturday }

// Multiples of 10 enumeration enum TensTable =

{

Ten = 10, Twenty = 20, Thirty = 30, Forty = 40, Fifty = 50,

Sixty = 60, Seventy = 70, Eighty = 80, Ninety = 90

}

A.2.5 DELEGATES

A delegate is a reference type that encapsulates one or more methods. A delegate is created with a defined method signature, and any method in any class or structure that adheres to this defined signature may be assigned to the delegate. Each method assigned to a delegate is referred to as a callable entity.

In the .NET Framework, a delegate is a class implicitly derived from the tem.Delegate class. Note that an instance of a delegate, since it is implicitly a class, has a default value of null.

Delegates are declared and used somewhat like function pointers in C++, except that delegates encapsulate both an object instance and a method. This encapsulation of the object as well as the method permits delegates to refer to both static and instance

TYPES

653

Соседние файлы в папке c#