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

C#

DISPLAY HEAP AND STACK INFORMATION

# allocates memory in one of two ways: heap and Cstack. The heap method provides more flexibility so

classes usually use the heap method. The stack approach sets aside memory for processing. Structs use stack memory allocation because they are self-contained and know exactly how much memory to allocate for their operation.

A heap memory method is a term that describes the dynamic allocation and freeing of objects as the program runs. The heap method is best when you do not know the amount of objects ahead of time and/or the number of objects cannot fit into a stack. Because classes produce a large number of objects that cannot be known ahead of time, the compiler allocates new classes and operators on the heap.

A stack is an area of memory that holds arguments and variables. When the compiler compiles your project it automatically sets aside the stack memory it will need so your program will run properly. Because structs are selfcontained, the compiler knows how much memory to use and sets aside the stack.

The heap method gives you more flexibility, and it is best when you use classes. However, you should use structs whenever possible to ensure that the amount of memory your project takes up is as low as possible, which means your project is reaching peak performance.

DISPLAY HEAP AND STACK INFORMATION

the file. The class1.cs code appears in the parent window.

ˇ Delete all code after the namespace Stack code.

Á Type the code that establishes your stack and displays the stack values.

PROGRAMMING C# BUILDING BLOCKS 4

Many performance factors depend on the platform that you run your program on. Most users run some flavor of Windows, and unfortunately Windows has yet to have perfect memory allocation. Depending on the version of Windows that you use, you may not get the performance that you expect or the same performance on every flavor of Windows.

The heap method of memory allocation can take time because the compiler is always opening, freeing up, and reorganizing memory blocks. Depending on how you construct your program, there may also be threads trying to access memory at the same time or other types of memory corruption that can cause your project (or even your computer) to crash.

There is no magic wand to fix heap memory problems, but Windows 2000, the most current version of Windows as of this writing, has the best memory allocation features. Windows XP promises to improve its memory allocation abilities. Program carefully so you do not have memory headaches no matter what Windows platform your project will run on.

Type the code that removes an element from the stack.

° Type the code that displays the first element in the stack.

· Type the code that displays the stack values

Type the code that outputs the stack properties and values.

Run the program by

± Save the program as the

pressing the F5 key.

filename.

The stack values appear at

 

the top followed by the

 

removed first string (Pop),

 

the new first string in the

 

stack (Peek) and the new

 

stack values.

 

85

C#

FIND TYPE INFORMATION

C# categorizes the elements that it uses to process information into types. Types indicate the elements within them and how they must be used. Because it

can be hard to remember the elements associated with certain types, the MDE window contains type information for your reference.

Four type categories exist: value, reference, pointer, and void. Types in each category exist that perform a specific function.

value types store data within your C# program. Two categories of types comprise value types: struct and enumeration. struct types contain structs and built-in simple types, including integral, floating-point, decimal, and Boolean types. The enumeration type lets you declare a set of named constants.

reference types store references to data elsewhere in your C# program. reference type keywords include class, interface, delegate, object, and string.

pointer types let you point to a specific location in memory. You can only use pointer types in unsafe mode, where you specifically instruct the Visual Studio .NET compiler not to manage memory for a particular block of code, such as a class.

You use the void type in a method to specify that the method does not return a value. The MDE window online help contains complete information about types if you are uncertain about what type you must use in a specific situation.

FIND TYPE INFORMATION

Index...

Ctrl+Alt+F2

.0

Microsoft Visual Studio.Net 7.0

The Start Page appears.

Click Index.

¤ Click Help.

 

WORKING WITH TYPES AND INTERFACES 5

The MDE window online help contains more detailed reference information about types where you can learn about all the variables that are available for

all the various types, particularly the value types. The reference information you can access includes the following help pages:

The Default Values Table displays all of the available value types and their default values.

The Built-In Types Table displays all of the built-in types and what their .NET counterpart keywords are. When the Visual Studio .NET compiler compiles your Visual C# project it converts the Visual C# types into the .NET keywords for final compilation.

The Language Equivalent: Types page displays a table that has the storage size of each type and the equivalent type names for Visual Basic, Java, C++, Visual C#, Jscript, and Visual FoxPro.

The Implicit Numeric Conversions Table and Explicit Numeric Conversions Table contained predefined implicit and explicit numeric conversion tables.

Index

The Index window appears.

Type types in the Look for field.

Note: You can expand the Index window by closing the Properties window.

ˇ Click the compared in different languages entry in the Index list box.

The Look for field displays types, compared in different languages.

The Language Equivalents: Types help page appears displaying the type differences between Visual C# and other languages.

87

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