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

C H A P T E R 12

Structs

What Are Structs?

Structs Are Value Types

Assigning to a Struct

Constructors and Destructors

Field Initializers Are Not Allowed

Structs Are Sealed

Boxing and Unboxing

Structs As Return Values and Parameters

Additional Information About Structs

317

CHAPTER 12 STRUCTS

What Are Structs?

Structs are programmer-defined data types, very similar to classes. They have data members and function members. Although similar to classes, there are a number of important differences. The most important ones are the following:

Classes are reference types, and structs are value types.

Structs are implicitly sealed, which means they cannot be derived from.

The syntax for declaring a struct is similar to that of declaring a class:

Keyword

struct StructName

{

MemberDeclarations

}

For example, the following code declares a struct named Point. It has two public fields, named X and Y. In Main, three variables of struct type Point are declared, and their values are assigned and printed out.

struct Point

{

public int X; public int Y;

}

class Program

{

static void Main()

{

Point first, second, third;

first.X = 10; first.Y = 10; second.X = 20; second.Y = 20; third.X = first.X + second.X; third.Y = first.Y + second.Y;

Console.WriteLine("first: {0}, {1}", first.X, first.Y); Console.WriteLine("second: {0}, {1}", second.X, second.Y); Console.WriteLine("third: {0}, {1}", third.X, third.Y);

}

}

318

CHAPTER 12 STRUCTS

Structs Are Value Types

As with all value types, a variable of a struct type contains its own data. Consequently:

A variable of a struct type cannot be null.

Two structs variables cannot refer to the same object.

For example, the following code declares a class called CSimple, a struct called Simple, and a variable of each. Figure 12-1 shows how the two would be arranged in memory.

class CSimple

{

public int X; public int Y;

}

struct Simple

{

public int X; public int Y;

}

class Program

{

static void Main()

{

CSimple cs = new CSimple(); Simple ss = new Simple();

...

Figure 12-1. Memory arrangement of a class versus a struct

319

CHAPTER 12 STRUCTS

Assigning to a Struct

Assigning one struct to another copies the values from one to the other. This is quite different from copying from a class variable, where only the reference is copied.

Figure 12-2 shows the difference between the assignment of a class variable and a struct variable. Notice that after the class assignment, cs2 is pointing at the same object in the heap as cs1. But after the struct assignment, the values of ss2’s members are copies of those in ss1.

class CSimple

{ public int X; public int Y; }

struct Simple

{ public int X; public int Y; }

class Program

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

static void Main()

 

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CSimple

cs1 = new CSimple(),

cs2 = null;

// Class instances

Simple

ss1 = new Simple(),

ss2 = new

Simple();

// Struct instances

cs1.X = ss1.X = 5;

//

Assign 5 to ss1.X and cs1.X

cs1.Y = ss1.Y = 10;

//

Assign 10 to ss1.Y and cs1.Y

cs2 = cs1;

//

Assign class instance

ss2 = ss1;

//

Assign struct instance

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 12-2. Assigning a class variable and a struct variable

320

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