- •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#
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
