- •maranGraphics
- •CREDITS
- •ACKNOWLEDGMENTS
- •ABOUT THE AUTHORS
- •AUTHORS’ ACKNOWLEDGMENTS
- •TABLE OF CONTENTS
- •HOW TO USE THIS BOOK
- •INTRODUCTION TO C#
- •START VISUAL STUDIO .NET
- •OPEN A NEW C# PROJECT
- •OPEN A C# WEB PROJECT
- •SET JSCRIPT .NET AS THE DEFAULT SCRIPT LANGUAGE
- •EXPLORE THE CLASS VIEW WINDOW
- •VIEW THE CONTENTS WINDOW
- •GET HELP USING THE INDEX WINDOW
- •SEARCH FOR HELP
- •ADD COMPONENTS FROM THE TOOLBOX
- •ADD A TASK TO THE TASK LIST
- •CHANGE FORM PROPERTIES IN THE PROPERTIES WINDOW
- •ADD A CUSTOM TOOLBAR
- •DELETE A TOOLBAR
- •CHANGE THE VISUAL STUDIO ENVIRONMENT
- •MANAGE OPEN WINDOWS
- •OPEN A PROJECT
- •VIEW THE MAIN METHOD
- •COMBINE PROGRAM TYPES
- •ADD REFERENCE TYPES
- •ADD OPERATORS
- •INSERT ATTRIBUTES
- •ENTER CLASSES
- •ADD COMMENTS TO CODE
- •WRITE YOUR FIRST PROGRAM
- •ENTER XML DOCUMENTATION
- •ACCESS DOCUMENTATION
- •LOG A BUG REPORT
- •VIEW INFORMATION ABOUT C# BUILDING BLOCKS
- •PROGRAM CLASSES
- •ADD A CLASS
- •EMPLOY CLASS INHERITANCE
- •PROGRAM INSTANCE CONSTRUCTORS
- •INSERT DESTRUCTORS
- •PROGRAM STRUCTS
- •DISPLAY HEAP AND STACK INFORMATION
- •FIND TYPE INFORMATION
- •PROGRAM CONSTANT EXPRESSIONS
- •SPECIFY VALUE TYPES
- •PROGRAM NUMERIC TYPES
- •PROGRAM THE BOOLEAN TYPE
- •DECLARE REFERENCE TYPES
- •ENTER REFERENCE TYPE DECLARATIONS
- •CONVERT VALUE TYPES TO REFERENCE TYPES
- •PROGRAM POINTER TYPES
- •INSERT THE VOID TYPE
- •ADD INTERFACE PROPERTIES
- •ADD AN INTERFACE INDEX
- •VIEW INFORMATION ABOUT METHODS
- •ADD A METHOD
- •ADD STATIC METHODS
- •INCLUDE NON-STATIC METHODS
- •ENTER DELEGATES
- •PROGRAM EVENTS
- •ADD AN EVENT-HANDLING METHOD
- •VIEW INFORMATION ABOUT ARRAYS
- •ENTER SINGLE-DIMENSIONAL ARRAYS
- •ADD MULTIDIMENSIONAL ARRAYS
- •PROGRAM ARRAY-OF-ARRAYS
- •ITERATE THROUGH ARRAY ELEMENTS
- •SORT ARRAYS
- •SEARCH ARRAYS
- •IMPLEMENT A COLLECTIONS CLASS
- •PROGRAM STRUCTS
- •ADD AN INDEXER
- •INCLUDE ENUMERATIONS
- •CREATE STRING LITERALS AND VARIABLES
- •ASSIGN VALUES TO STRINGS
- •CONCATENATE STRINGS
- •COMPARE STRINGS
- •SEARCH FOR SUBSTRINGS
- •REPLACE CHARACTERS
- •EXTRACT SUBSTRINGS
- •CHANGE THE CHARACTER CASE
- •TRIM SPACES
- •REMOVE CHARACTERS
- •SPLIT A STRING
- •JOIN STRINGS
- •PAD STRINGS
- •VIEW INFORMATION ABOUT PROPERTIES
- •COMPARE PROPERTIES AND INDEXERS
- •PROGRAM PROPERTY ACCESSORS
- •DECLARE ABSTRACT PROPERTIES
- •INCLUDE PROPERTIES ON INTERFACES
- •VIEW INFORMATION ABOUT WINDOWS FORMS
- •ADD A WINDOWS FORM IN THE WINDOWS FORM DESIGNER
- •SET THE FORM TYPE
- •CHOOSE THE STARTUP WINDOWS FORM
- •CREATE A MODAL FORM
- •LAYOUT A FORM
- •SET A FORM LOCATION
- •CHANGE FORM PROPERTIES
- •CREATE A TRANSPARENT FORM
- •AN INTRODUCTION TO WEB FORMS AND CONTROLS
- •CREATE AN ASP.NET WEB SITE
- •CREATE A WEB FORM
- •ADD SERVER CONTROLS TO A WEB FORM
- •READ AND CHANGE PROPERTIES FROM OBJECTS ON A WEB FORM
- •USING SERVER-SIDE COMPONENTS ON WEB FORMS
- •INTRODUCING DATA ACCESS WITH ADO.NET
- •DISPLAY DATA WITH THE DATAGRID CONTROL
- •CONFIGURE THE DATAGRID CONTROL
- •INSERT DATA INTO A SQL DATABASE
- •UPDATE DATA FROM A SQL DATABASE
- •DELETE DATA FROM A SQL DATABASE
- •EXECUTE A STORED PROCEDURE IN A SQL DATABASE
- •READ XML FROM A FILE
- •SAVE XML TO A FILE
- •QUERY XML WITH XPATH
- •APPLY XSL TO XML
- •INTRODUCTION TO DISTRIBUTED APPLICATIONS
- •CREATE AN APPLICATION WITH PRIVATE ASSEMBLIES
- •CREATE AN APPLICATION WITH SHARED ASSEMBLIES
- •VERSION A SHARED ASSEMBLY
- •CONFIGURE A CLIENT FOR A VERSIONED ASSEMBLY
- •CREATE A WEB SERVICE
- •USING A WEB SERVICE
- •INTRODUCTION TO EXCEPTION HANDLING
- •THROWING AN EXCEPTION
- •HANDLING EXCEPTIONS WITH THE CATCH BLOCK
- •USING THE FINALLY BLOCK
- •WRITE ERRORS TO THE APPLICATION LOG
- •BASIC EXAMPLES
- •WHAT’S ON THE CD-ROM
- •USING THE E-VERSION OF THIS BOOK
- •INDEX
- •Symbols & Numbers
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