- •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 REFERENCE TYPE DECLARATIONS
Visual C# offers three different keywords for declaring reference types: class, interface, and delegate. The class, interface, and delegate types have
similar statement structures. They include optional class attributes and modifiers that further define your reference type and the identifier, which is the name of your reference type. After that the options change depending on the reference type you use. For example, with classes, you have the ability to specify a base class and any class member declarations. An interface and a class are also closely related in that they can rely on base versions of themselves that contain basic data but no members.
A class contains references about data. In contrast, an interface contains references about how that data should be used — that is, what methods, properties, events, and indexers should apply to that data. Interfaces contain only abstract members that have basic information about how data in a class or struct should behave.
Classes and structs can apply to more than one interface, and the class and/or struct must adhere to that interface much like you must adhere to a contract that you sign.
ENTER REFERENCE TYPE DECLARATIONS
Properties
Project. ■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for the file.
ˇ Click OK.
WORKING WITH TYPES AND INTERFACES 5
To save keystrokes, you can implement an interface directly from a class.
TYPE THIS:
using System; interface IntBase1
{
void IBMethod1();
}
interface IntBase2
{
void IBMethod2();
}
interface Int1: IntBase1, IntBase2
{
void Method1();void Method2();
}
interface Int2: IntBase1, IntBase2
{
void Method3(); void Method4(); void Method5();
}
class Class1: Int1, Int2
{
public static void Main()
{
Console.WriteLine("This class inherits from two interfaces that inherit from two base interfaces. No values are returned because all the interfaces do are return void methods.");
}
RESULT:
This class inherits from two interfaces that inherit from two base interfaces. No values are returned because all the interfaces return void methods.
■ The appears window
Á Delete |
CONTINUED |
within |
|
C#
ENTER REFERENCE TYPE DECLARATIONS
he delegate reference type serves two functions. First, Ta delegate object serves as the primary object in an
event. An event tells your project about something that happens to an object in your program. Second, the delegate object contains method information that tells the affected object in the event what to do when the event occurs.
Delegates act like function pointers in other languages such as C++ and Pascal. Unlike other languages, Visual C# delegates are completely object-oriented so they are secure and type-safe. Type-safe code is code that accesses types in well-defined ways so as to prevent crashing programs that
can lead to other nasty things such as memory leaks and crashing operating systems.
When you create a delegate, you must enter two mandatory options. First, you must enter the result type that matches the return type of the method. Entering the result type lets you tie in the delegate with the method. Second, you must enter the delegate name. Without either of those options, the MDE window calls your attention to the error. You can add attributes and modifiers as you can with classes and interfaces.
ENTER REFERENCE TYPE DECLARATIONS (CONTINUED)
■ The New Project window appears.
¡ Click the Console Application icon in the Templates pane.
™ Type a name for the file.
£ Click OK.
WORKING WITH TYPES AND INTERFACES 5
No matter if you write your delegate before or after you write your method, avoid compilation errors by ensuring that the delegate result type and your method return type match before you compile your project.
The greatest similarity between delegates and interfaces is that they separate the specification of methods with the implementation of those methods. As with the class and struct, your decision about using a delegate or an interface depends on what you are trying to do.
If you need to call a single method or you want a class to refer to several methods, use the delegate. The delegate also has the added advantage of being easier to construct than the interface. However, the interface lets you specify the methods that an object in your project calls instead of general methods that a delegate includes. The interface is also a good choice if a class needs an inheriting interface as a jump point for accessing other interfaces or classes.
|
|
|
|
|
■ The Class1.cs code |
|
|
|
|
∞ Type the code that |
||||
appears in the parent |
establishes the delegate, calls |
|||
window. |
the delegate, and outputs the |
|||
¢ Delete the comments |
result. |
|||
|
|
|
||
within the Main method. |
|
|
|
§ Run the program by |
¶ Save the program as the |
pressing the F5 key. |
filename. |
■ The constant expression |
|
appears onscreen. |
|
103