Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

CONVERT VALUE TYPES TO REFERENCE TYPES

isual C# enables you to convert value types to Vreference types and vice versa with a process called

boxing. Boxing refers to the value type to reference type conversion process. Unboxing is the reverse procedure that converts reference types to value types.

Visual C# boxes value types, including struct and built-in value types, by copying the value from the value type into the object. After you box the value type, you can change the value of that value type. Boxing is useful when you need to copy a value from one value type to one or more value types. For example, you can copy an integer value to one or

more integers by having those other integers reference the object you created when you boxed the integer value.

Unboxing lets you convert an object into a value type or an interface type into a value type that implements that interface. When Visual C# unboxes the object, it checks the object to see if it is the same value type as the one you specify in the unboxing argument. If Visual C# sees that this is true, it unboxes the object value and places it into the value type.

CONVERT VALUE TYPES TO REFERENCE TYPES

Console

Applicatio

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

WORKING WITH TYPES AND INTERFACES 5

You can unbox an object with a boxed object value. For example, if you see an object statement with a = 5, you want to move the number 5 from the object to an integer, or else the compiler will return an error. You can test whether an object value has been boxed correctly using the try and catch arguments.

TYPE THIS:

using System; public class Unbox

{

int a = 5;

object x = a // boxes a into object x try

{

int b = (int) x;

Console.WriteLine("The integer unboxed successfully.");

}

catch (InvalidCastException e) // If there is an error, the catch argument catches it.

{

Console.WriteLine("{0} Unboxing error!",e);

}

}

RESULT:

The integer unboxed successfully.

Á

C#

PROGRAM POINTER TYPES

hen you compile a project, the Visual Studio .NET Wgarbage collector manages all objects in your class

and ensures that all objects handle memory correctly and have legitimate references. However, there may be times when you need to have an object access a particular memory address that you do not want the garbage collector to touch. Visual Studio .NET gives you this control with unsafe mode and pointers.

When you enter the unsafe keyword in code, you tell the compiler and the Visual Studio .NET runtime environment (the Common Language Runtime) that the garbage collector should not manage those memory blocks that have been allocated in the unsafe argument. You point to the memory blocks to reserve by using the pointer type.

The key portion of your unsafe code block is the fixed pointer type. The fixed pointer type pins down the memory you want to reference so the garbage collector will not allocate that memory block at random to other objects in your program.

Note that if you try to create pointer types and do not explicitly create the unsafe context in your code, the pointers will be considered invalid. In that case the MDE window will alert you to this error, and if you try to compile your project, the compiler will return an error.

PROGRAM POINTER TYPES

Console

Applicatio

Properties

¤ Click New Project.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The New Project window

 

 

 

 

 

 

 

 

 

 

Type a name for the file.

 

appears.

 

ˇ Click OK.

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

 

 

 

Templates pane.

 

 

 

 

 

WORKING WITH TYPES AND INTERFACES 5

You can initialize pointers of different types by nesting fixed statements within each other. This approach saves time when you need to declare several different pointer types.

TYPE THIS:

 

RESULT:

using System;

2

class Pointer

4

{

6

int x, y;

 

 

unsafe static void Main()

 

 

{

 

 

Pointer test = new Pointer();

 

 

Fixed(int* p1 = &test.x)

 

 

Fixed (int* p2 = &test.y)

 

 

*p1 = 2;

 

 

*p2 = 4;

 

 

Console.WriteLine(test.x);

 

 

Console.WriteLine(test.y);

 

 

}

 

 

If you receive an error running unsafe code you have not told the compiler to compile unsafe code. You can do so by selecting the project name in the Solution Explorer window and pressing Shift+F4 on your keyboard. When the Property Pages window appears, you can click the Configuration Properties file folder in the left-pane and then change the Allow unsafe code blocks setting to True.

}

The appears window

Á Delete within

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