Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

 

Chapter 9 Creating Value Types with Enumerations and Structs

183

Chapter 9 Quick Reference

 

 

 

To

Do this

 

 

 

Declare an enumeration

Write the keyword enum, followed by the name of the type, followed

 

 

 

by a pair of braces containing a comma-separated list of the

 

 

 

enumeration literal names. For example:

 

 

 

enum Season { Spring, Summer, Fall, Winter }

 

 

Declare an enumeration variable

Write the name of the enumeration on the left followed by the name

 

 

 

of the variable, followed by a semicolon. For example:

 

 

 

Season currentSeason;

 

 

 

Assign an enumeration variable

Write the name of the enumeration literal in combination with the

 

 

to a value

name of the enumeration to which it belongs. For example:

 

 

 

currentSeason = Spring;

// error

 

 

 

currentSeason = Season.Spring; // correct

 

 

Declare a structure type

Write the keyword struct, followed by the name of the structure type,

 

 

 

followed by the body of the structure (the constructors, methods, and

 

 

 

fields). For example:

 

 

 

 

struct Time

 

 

 

 

{

 

 

 

 

public Time(int hh, int mm, int ss)

 

 

 

{ ... }

 

 

 

 

...

 

 

 

 

private int hours, minutes, seconds;

 

 

 

}

 

 

 

Declare a structure variable

Write the name of the structure type, followed by the name of the

 

 

 

variable, followed by a semicolon. For example:

 

 

 

Time now;

 

 

 

Initialize a structure variable

Initialize the variable to a structure value created by calling the struc-

 

 

to a value

ture constructor. For example:

 

 

 

Time lunch = new Time(12, 30, 0);

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