- •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#
SORT ARRAYS
method lets you sort elements array. You can use the
not only with single-
also with jagged arrays because more than one single-dimensional
the IComparable or IComparer in the array implements. The
interfaces are defaults that C# with the Array.Sort method, so
about programming the interface
When you sort an array, C# orders the elements in that array in alphabetical order for strings and in numerical order for numerical types. You can then tie in the sort to a Console.WriteLine statement as shown in the task example so you can see how C# will sort the arrays.
For example, if you have a set of strings as your elements, and you want to write your sorted elements to the screen, the output will show the string with the first letter closest to the letter a and continue on in the list. If you have a numeric list, then the first number in the output will be the one that has the lowest amount, even if that amount is a negative number.
SORT ARRAYS
Properties
⁄ Click Start Programs |
|
|
|
|
|
■ The Start page appears. |
■ The New Project window |
||||
Microsoft Visual Studio .NET |
|
¤ Click New Project. |
appears. |
||
|
|||||
7.0 Microsoft Visual |
|
|
|
‹ Click the Console |
|
Studio .NET 7.0. |
|
|
|
||
|
|
Application icon in the |
|||
|
|
|
|||
|
|
|
Templates pane. |
› Type a name for the file.
ˇ Click OK.
146
When you sort arrays that have strings that contain capital letters, C# considers those strings to be lower on the alphabetization list than strings with lowercase letters.
TYPE THIS:
using System; class SortArray;
{
public static void Main()
{
string[] names = {"too", "two", "To", "Too"}; Array.Sort(names);
foreach (string value in names)
{
Console.WriteLine("The word is {0}", value);
}
}
}
USING ARRAYS 7
RESULT:
The word is too
The word is two
The word is To
The word is Too
Á Type the code that creates an instance of your array, the elements in your array, the sort method, and outputs the results.
‡ Type the Output method that outputs the information to the screen.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The sorted array elements |
|
with their associated array |
|
locations appear on the |
|
screen. |
|
147
C#
SEARCH ARRAYS
C# lets you search for the first instance of an element in an array in case you need to pass a particular element in your array to another part of your
program or if you need to get some specific information such as finding the number of times an element appears in an array.
You can search within an array using the Array.IndexOf method. This built-in method returns the index number of the first array element that you want to search for. For example, if you search for the third element in an array, then the Array.IndexOf method returns the index
number 2 because the default first index number in an array is 0. If you set the first index number yourself, then the index number returned for your found element will vary.
The Array.IndexOf method also lets you search for an array element within certain index positions. For example, you can search for an array element that is the string and that appears between index number 2 and 10. You can also search for an array element from an index position through the last element in the array.
The drawback to using the Array.IndexOf method is that you can only search within a single-dimensional array.
SEARCH ARRAYS
Properties
|
|
|
|
|
|
|
■ The Start page appears. |
■ The New Project window |
|||
.NET |
|
¤ Click New Project. |
appears. |
||
|
|||||
|
|
|
|
‹ Click the Console |
|
|
|
|
|
||
|
|
|
Application icon in the |
||
|
|
|
Templates pane. |
› Type a name for the file.
ˇ Click OK.
USING ARRAYS 7
You can use the Array.LastIndexOf method to find the last occurrence in an array.
TYPE THIS:
using System; public class Sample
{
public static void Main()
{
array Sample=Array.CreateInstance( typeof (String), 3); Sample.SetValue( "Five", 0 );
Sample.SetValue( "by", 1 );
Sample.SetValue( "Five", 2 ); string String1 = "Five";
int Index1 = Array.LastIndexOf( Index1, String1 );
Console.WriteLine("The last occurrence of \"{0}\" is at index {1}.", String1, Index1);
}
}
RESULT:
The last occurrence of “Five” is at index 2.
Á Type the code that creates an instance of your array, the elements in your array, the
Array.IndexOf search method, and outputs the results.
‡ Type the Output method that outputs the information to the screen.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The element and its |
|
associated number appear |
|
on the screen. |
|
149