- •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#
ENTER SINGLE-DIMENSIONAL ARRAYS
Single-dimensional arrays let you define a set of variables and store them in one block of memory for easy retrieval. C# single-dimensional arrays can
include defined sets of data. For example, you can enter a set of numbers or a set of string values such as the number of months in a year. You can use any value type as part of your array including integral, floating point, decimal, Boolean, struct, and enumeration.
After you declare your array, you must initialize it by using the new operator. When you initialize the array, you can give the array specific values such as numbers or specify the maximum number of elements in the array.
You give the array specific values as you do in C, C++, and Java — placing the values in curly braces at the end of the array argument. If you specify the maximum number of elements in an array instead, C# automatically assigns the first element in your array the number zero. For example, an array with six elements will be numbered 0 through 5.
Accessing an array in C# is very similar to what you find in C and C++. For example, you can create an integer array, then assign an integer to a particular location in that array.
ENTER SINGLE-DIMENSIONAL ARRAYS
Properties
page appears. ■ The New Project window
appears.
Project.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
USING ARRAYS 7
You can omit optional parts of the single-dimensional array argument. One way is to omit the size of the array.
int[] values = new int[] {1, 2, 3, 5, 7, 11};
string[] letters = new string[] {"A", "B", "C"};
Another way is to omit the new statement altogether.
int[] values = {1, 2, 3, 5, 7, 11};
string[] letters = {"A", "B", "C"};
Á Type the code that outputs the opening string and establishes the array in the Main method.
‡ Type the code that outputs the array by iterating through each element using the foreach statement.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The prime number array |
|
appears on the screen. |
|
139
C#
ADD MULTIDIMENSIONAL ARRAYS
C# lets you declare multidimensional arrays for processing a large number of values in one argument. A multidimensional array arranges its data
similar to the way a spreadsheet does.
C# multidimensional arrays let you specify two or three elements in the array for two-dimensional and threedimensional arrays, respectively. You can use twodimensional arrays for specifying coordinates such as with the row and column in a spreadsheet, on a map, or on a game board such as those for checkers and chess. Programmers use two-dimensional arrays for such tasks as image processing.
A three-dimensional array lets you specify three elements. For example, you can store a name in three dimensions — first name, middle name, and last name.
Just as with single-dimensional arrays, you can specify the number of elements in each dimension in the rectangular brackets after you declare the array type. If you think of the array as the table, C# lists the number of rows first and the number of columns second. If you have a three-dimensional array, then the third dimension appears last in the bracket.
You can also specify initial values for the array in the same order as you have them in the rectangular brackets. Like single-dimensional arrays, values appear in curly braces after you initialize the array with the new operator.
ADD MULTIDIMENSIONAL ARRAYS
Properties
page appears. ■ The New Project window
appears.
New Project.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
USING ARRAYS 7
C# contains rules about array structure that you must adhere to so your array can function properly. The rules include:
•Specify the size of your dimensions when you create a multidimensional array. If you have a particular array with the value x, you can specify the size of x-1 dimensions, but it is usually safer, not to mention less confusing, if you specify the information up front.
•When you create the array, it is a good idea to keep the same dimensions for every other array in your program. This approach can reduce confusion for users of your program.
•Microsoft recommends that you use the first dimension as the row and the second as the column, but you can define your dimension order however you like. Your dimension order must be consistent throughout your program.
Á Type the code that outputs the array.
‡ Type the Main method that establishes the array.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ Type information at the |
|
prompts and the output |
|
appears. |
|
141