Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
39
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

C#

INSERT DESTRUCTORS

hen you create constructors you are setting the Wstage to place your objects somewhere in memory.

However, there may be times where you have to remove those objects from memory either to free up the space for other objects or because you want to clean out those objects that no longer apply to your program. C# gives you the ability to delete these objects by using destructors.

As the name suggests, a destructor destroys objects that you specify. The good news is that C# employs destructors automatically when it discovers that an object is no longer being used by the code.

Destructors are also helpful when you have objects that take up absolute addresses in your memory. The end result is that you have cleaner code that runs more efficiently and you do not have to go on a search and destroy mission. The lack of explicit destructors is a bit of bad news, but because C# takes care of it, you have one less thing to worry about.

When your program compiles, C# checks to see if any code in your program does not use a particular object any longer. If C# finds such an instance it adds the destructor code with the void return type automatically.

INSERT DESTRUCTORS

Console

Applicatio

¤ Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING C# BUILDING BLOCKS 4

C# destroys objects completely and thoroughly. Destructors are not inherited — that is, when C# determines that your project is no longer using the object in a base class, it will not go to any other inherited classes to see if the objects exist in those inherited classes. Instead, C# goes through every class one by one. If C# finds an inherited class with the same object, then C# places that object higher on its list of objects to destroy.

After C# finishes its check of objects in all classes, it creates a list of objects to destroy with the objects in inherited classes first on its task list. Then C# goes through its list and destroys the orphan objects one by one. This all happens behind the scenes, but when you open your classes after your project compiles, you can see the destructor code.

The Class1.cs code appears in the parent window.

Á Delete all code after the namespace Destruct code.

Type the code that establishes the constructor, then destroys the constructor with the destructor, and outputs a report.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The string appears on the

 

screen.

 

79

C#

PROGRAM STRUCTS

Astruct is a value type that is short for structure. As you may have guessed, a structure contains many different types of data including constants,

constructors, fields, methods, and properties. A struct differs from a class in that a class is a reference type where an object created in your program refers to the class information to which the object belongs.

In contrast, a struct contains all of the information the object needs within itself. A struct is most useful if you have a limited range of values for a particular object such as the color of a cat’s fur or the types of model trains you have

available. The Visual Studio .NET team at Microsoft recommends that if you have a class that is smaller than 16 bytes then your C# program is more likely to handle a struct more efficiently than a class.

The structure of your struct code block is very similar to that of a class code. For example, a struct uses the same accessibility modifiers that let you determine how your project and other programs access your struct code.

However, you build your struct code within the main portion of your program and not as a separate class file.

PROGRAM STRUCTS

Console

Applicatio

¤ Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING C# BUILDING BLOCKS 4

Structs have one important limitation — they cannot inherit from another struct or class the way that a class does. You can implement an interface from a struct just as you do from a class.

TYPE THIS:

using System; class BaseClass

{

public static void Main()

{

Console.WriteLine("The base class.");

}

}

struct Inherit : BaseClass

RESULT:

An error in the MDE window appears because you cannot have a struct inherit from a base class.

The Class1.cs code appears in the parent window.

Á Delete all code after the namespace Structs code.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Type the code that

 

° Type the code that sets the

 

establishes the struct and its

coordinates.

variables.

 

 

 

CONTINUED

81

C#

PROGRAM STRUCTS

tructs are more efficient when you have only a limited Srange of values that an object must refer to. This makes

a struct a good choice when you define an array of values because a struct will process only the array, not each separate value in the array.

Unlike classes, C# does not include a Struct Wizard like the Add Class Wizard, which helps you create classes. What is more, when you create structs you do not do so in its own component as with classes. Instead, you create structs within the main body of your project programmatically.

Structs can include constructors like classes can, but these struct constructors must include parameters. These

parameters include the name of the struct and if the struct depends on or implements an interface. If you try to create a struct that has no parameters, C# will let you know that you are in error and your project will not compile.

There are some other differences between structs and classes. There is also no inheritance for structs as there is for classes because structs are self-contained. Structs cannot inherit information from most classes, and structs cannot function as a base class.

PROGRAM STRUCTS (CONTINUED)

Type the output line for the Start coordinates.

PROGRAMMING C# BUILDING BLOCKS 4

You can create a built-in union attribute in C# so that all fields in your program start at the same point in memory.

TYPE THIS:

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Union] // Place the struct attribute before declaring the struct.

struct Union

RESULT:

Declaring your struct information and the System.Runtime.InteropServices namespace ensures that you can run your program. After you declare your struct you can enter the struct constructor.

Type the output line for the End coordinates.

± Run the program by

¡ Save the program as the

pressing the F5 key.

filename.

The string appears on the

 

screen.

 

83

Соседние файлы в предмете Программирование на C++