- •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#
IMPLEMENT A COLLECTIONS CLASS
Acollections class collects a number of elements that have a specific type, such as a set of numbers that represent the months of the year. C# provides two
methods for declaring collections classes: programming arrays and programming the built-in IEnumerator and IEnumerable interfaces.
An array is built from the System.Array base class that is built into C#. C# identifies this base class as a collections class. You can also define a class as a collections class provided that you declare the System.Collections namespace in your program and include the IEnumerator and IEnumerable interfaces within the class.
The IEnumerator and IEnumerable interfaces let you enumerate elements in your collections class. Enumerations are discussed on page 156, but as a sneak preview, enumerations assign numbers to elements in your collections class so you and your program can keep track of your elements more easily.
Like an array, you can retrieve information from a collections class using the foreach statement. The foreach statement works on a collections class the same way it works in an array — the foreach statement iterates through each element in the collections class and can return that information to another statement or method in your program such as the Console.WriteLine statement for output.
IMPLEMENT A COLLECTIONS CLASS
Project window
Console icon in the
pane.
name for the file.
.
Á Delete all code after the left brace directly below the namespace Implement code.
‡ Type the code that establishes the array, establishes the
GetEnumerator definition, and defines part of the
Enumerator class.
USING ARRAYS 7
Like an array, you can use the foreach statement for iterating through a collections class. The following example acquires a collection in a hashtable, a predefined collection class.
|
TYPE THIS: |
|
|
RESULT: |
|
|
|
|
|
||||
|
|
|
|
|
|
|
|
using System; |
|
|
209 |
Stockton |
|
|
using System.Collections; |
|
|
559 |
Fresno |
|
|
public class Class1 |
|
|
916 |
Sacramento |
|
|
{ |
|
|
|
|
|
|
public static void Main(string[] args) |
|
|
|
|
|
|
{ |
|
|
|
|
|
|
Hashtable areacode = new Hashtable(); |
|
|
|
|
|
|
areacode.Add("209", "Stockton"); |
|
|
|
|
|
|
areacode Add("559", "Fresno"); |
|
|
|
|
|
|
areacode Add("916", "Sacramento"); |
|
|
|
|
|
|
foreach (string code in areacode.Keys) |
|
|
|
|
|
|
{ |
|
|
|
|
|
|
Console.WriteLine(code + " |
" + areacode[code]); |
|
|
|
|
|
} |
|
|
|
|
|
|
} |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
° Type the remainder of the |
|
|
|
|
|||
· Type the MainClass |
||||||||
|
||||||||
Enumerator class code. |
code that iterates through the |
|||||||
|
|
|
|
collections class and outputs |
||||
|
|
|
|
its elements. |
‚ Run the program by |
— Save the program as the |
pressing the F5 key. |
filename. |
■ The collections class |
|
elements appear on the |
|
screen. |
|
151
C#
PROGRAM STRUCTS
The struct is a close relative of the class. A struct can have the same members of a class and can also implement interfaces. However, a struct is a value type
so it will simply process information, such as integers passed through an array, as any other value type instead of instantiating objects for each element in the array as a class would. Using structs can save memory and help your program run faster.
You create an object in the struct by using the new operator. After you create the object, C# will create the object and call the value for the object. For example, you
can create an integer object that gets its value from a method contained in a class.
Because a struct is a value type, you cannot inherit from other structs and you cannot use a struct as a base class. A struct can inherit from an object in a base class but not from any inheriting classes.
When you create and run a program with a struct, C# creates the struct on the memory stack instead of the heap. Structs use attributes for specifying the memory areas the struct accesses. C# contains several different built-in struct attributes that you can use for certain tasks.
PROGRAM STRUCTS
■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
Á Delete all code after the left brace directly below the namespace Struct code.
‡ Type the struct property values.
The struct attributes mentioned in this task are different from the value type attribute modifiers that determine the accessibility of your struct. You enter the attribute information immediately before you enter the struct declaration, and the attribute appears within closed square brackets ([]).
TYPE THIS:
Using System;
[StructLayout(LayoutKind.Union)]
struct Union
{z
// Add struct information here.
}
USING ARRAYS 7
RESULT:
This code establishes a struct that contains the
StructLayout (LayoutKind.Union) attribute.
° Type the output code and the Main method that contains the struct value.
· Run the program by |
‚ Save the program as the |
pressing the F5 key. |
filename. |
■ The struct value appears |
|
on the screen. |
|
153