- •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#
ADD REFERENCE TYPES
C# categorizes elements refer to data elsewhere in a program as reference types. Reference types let you access data that you need within different places
in your program. For example, you may have several blocks of code that need to refer to the boiling temperature of water.
The reference type category contains several smaller categories including declarative and built-in types.
Declarative reference type elements include classes, interfaces, and delegates. These elements contain values and code that performs certain functions such as arithmetical operations.
Built-in reference types include objects and strings. An object is a collection of data and functionality. For example,
an object can be a variable with a value assigned to it, such as x = 1.
A string is a collection of characters for displaying output on screen. With string reference types, you can compare the values of the strings using the Visual C# equality operators — the == or =! operators — or other operators such as the additive operator, the plus sign, +. For example, you can define two strings and see if they are equal as shown below:
string a = "Tigger"
string b = "is a cat."
Console.WriteLine ( a + b );
The above code block would return with the output
Tigger is a cat.
ADD REFERENCE TYPES
¤ Click New Project in the Start page.
■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for your file.
ˇ Click OK.
WORKING WITH VISUAL C# BASICS 3
You can reverse the boxing process by using a process called unboxing. Unboxing converts an object to a value type. When Visual C# unboxes an object, it checks the object instance to make sure that the instance is the boxed value of the given value type (such as an integer), and then Visual C# copies the value of the instance into the value type variable.
TYPE THIS:
using System; public BoxClass
{
public static void Main()
{
int TiggerAge = 11;
object box = TiggerAge; // boxes the TiggerAge
value
int UnBoxedAge = (int)box; // Unboxes the
value
Console.WriteLine("The unboxed value is {0}", UnBoxedAge);
}
}
RESULT:
The unboxed value is
11.
|
|
|
|
|
■ The Class1.cs code |
|
‡ Type the code that |
||
|
||||
appears in the parent window. |
specifies strings and |
|||
Á Delete the comments |
concatenates them. |
|||
|
|
|
||
within the Main method. |
|
|
|
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The concatenated string |
|
appears on the screen. |
|
43
C#
ADD OPERATORS
Your program cannot operate without operators, which are mathematical symbols that perform a wide variety of functions. These operators compare, combine, and
contrast values so your program can make choices. For example, your program can refuse to perform a particular action if a user-entered value such as a password is not the same as a stored password in a program.
C# places operators into sixteen different categories. Some operators calculate arithmetical algorithms such as number addition and subtraction. Some arithmetical operators let you control calculation overflow errors, such as divide by zero errors, that can cause a program crash.
Some operators are logical — they calculate whether a condition is true or not such as a user ID number matching the ID number on file within the program. Other operators are relational and determine whether a value is greater than, equal to, or less than another value.
Other operators assign values to variables by using the equals sign or a combination of the equals sign and another operator. For example, if you have the arguments x = 1 and x + = 6, then that is the equivalent of x = 1 + 6.
The most important operator of all is the new operator that lets you create new objects, such as classes and variables, in your program.
ADD OPERATORS
Console
Applicatio
¤ Click New Project in the
.NET |
Start page. |
■ The New Project window appears.
‹ Click the Console Application icon in the Templates pane.
› Type a name for your file.
ˇ Click OK.
WORKING WITH VISUAL C# BASICS 3
Visual C# gives you the ability to overload operators. You can create your own operations when one or both of the operands are of a user-defined class or struct type.
|
|
TYPE THIS: |
|
|
RESULT: |
|
|
|
|
|
|||||
|
|
using System; |
|
|
Multiply Value: 30 |
|
|
|
|
class Multiply { |
|
|
|
|
|
|
|
int number; |
|
|
|
|
|
|
|
public Integer(int number) { |
|
|
|
|
|
|
|
this.number = number; } |
|
|
|
|
|
|
|
public static Multiply operator *(Multiply |
x, Multiply y) { |
|
|
|
|
|
|
return new Multiply(x.number * y.number); } |
|
|
|
|
|
|
|
class Output { |
|
|
|
|
|
|
|
public static void Main() { |
|
|
|
|
|
|
|
Multiply a = new Multiply(3,5); |
|
|
|
|
|
|
|
Multiply b = new Multiply(1,2); |
|
|
|
|
|
|
|
Console.WriteLine("Multiply Value: {0}", (a * b)); } |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
■ The Class1.cs code appears in the parent window.
Á Delete the comments within the Main method.
‡ Type the code that specifies integer values and combines them using four arithmetic operators.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The concatenated string |
|
appears on the screen. |
|
45