- •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#
PROGRAM ARRAY-OF-ARRAYS
The most flexible type of array is the array-of-arrays, commonly called the jagged array. The jagged array lets you define an array with several different
dimensions and sizes.
Multidimensional arrays have two or three dimensions that you can enter within the same rectangular braces that appear after the array value type. Array-of-arrays, however, let you nest single-dimensional arrays within one another. This approach lets you access a large number of arrays without the three-dimensional limit that multidimensional arrays provide.
When you initialize your array-of-arrays with the new operator, you must ensure that the number of brackets after
the new operator matches the number of brackets that appears after the array value type. If you do not, the MDE window will report the error.
Each single dimensional array must appear in its own rectangular braces that appear one after the other. You can also specify the initial element values in curly braces just as you do with single and multidimensional arrays. When you specify array values, you must ensure that the number of element values is the same as the number of arrays you specify after the array value type. For example, if you have four arrays, then you must specify four initial element values.
PROGRAM ARRAY-OF-ARRAYS
Properties
Start page appears. ■ The New Project window
appears.
Click New Project.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
USING ARRAYS 7
Multidimensional arrays also go by the name of rectangular arrays, and if you have programmed in other languages, you may have seen these arrays referred to as ragged arrays. Microsoft has discarded the ragged moniker and has instead moved it over to the array-of-arrays corner. What is more, Microsoft changed ragged to jagged, though the change in name is only a means to set Microsoft and C# apart from other languages, because there is no change in definition from ragged to jagged.
C# refers to array-of-arrays as jagged because if you visualize the array as with in a multidimensional array, a jagged array is a series of rows for each single-dimensional array that looks like a bar chart. The height of each array bar depends on the number of elements in that array. All the bars in your array “chart” would not be of uniform height — in other words, jagged.
Á Type the code that establishes the array.
‡ Type the code that iterates through the array and outputs the array elements that correspond with the array number.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The jagged array elements |
|
appear as members of their |
|
associated array number. |
|
143
C#
ITERATE THROUGH ARRAY ELEMENTS
After you program an array, you may need to iterate through array elements in case you need to list all of them for another part of your program or in your
output. C# lets you iterate through array elements by using the foreach statement.
The foreach statement is an easy way for you to display all of the elements contained in the class. The foreach statement acts as a loop that retrieves each array element. The loop follows the order that the elements appear in the array, and after the loop runs out of elements, the program moves on to the next statement.
The foreach statement appears immediately after the array statement. For example, you can view all of the elements in an array by assigning a Console.WriteLine statement after the foreach statement so you can see all of the array elements when your program runs. Another example is passing along integers from your array to a mathematical formula for further processing.
An array is a collections class that uses the System.Array base class. You can use the foreach statement for both arrays and collections. See page 150 for more information on implementing a collections class.
ITERATE THROUGH ARRAY ELEMENTS
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 also iterate through an array using the for statement if you want. The for statement requires you to match the array with the indexing operation whereas the foreach statement does not.
TYPE THIS:
using System; class Class1;
{
public static void Main()
{
int odd = 0, even = 0;
int[] arr = {1, 2, 3, 5, 7, 11};
for (int Index = 0; Index < arr.Count; Index++)
{
if (i%2 == 0) even++;
else odd++;
}
Class1 number = (Class1) arr[Index];
Console.WriteLine("There are {0} odd numbers and {1} even numbers.", odd, even);
}
}
RESULT:
There are 5 odd numbers and 1 even number.
Á Type the code that establishes the array.
‡ Type the foreach argument that squares each of the elements in the array and outputs that information to the screen.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The element square |
|
information appears on the |
|
screen. |
|
145
