- •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 COMMENTS TO CODE
C# lets you add comments to your code so you can remind yourself and tell others who view your code what you have done. If you update your code,
comments can tell you what you did when you wrote that string of code. Comments also tell others what you did to the code in case someone else has to review or change your code. In sum, comments show that you are a polite programmer.
Like Java and C++, Visual C# indicates comments with two forward slash marks (//). Visual C# also marks comments in green and automatically adds comments to your code if you
create a new Visual C# project that is something other than an empty project (such as a form or console application). These comments provide information about what certain code does and also include “to do” information about what code you should add to the program skeleton.
If you have only a few words in your comments, you can add the comments to the right of the code line. If you want to say more, you can add multiple lines, although it is usually a good idea to break multi-line comments out onto their own lines so people can read your comments more easily.
ADD COMMENTS TO CODE
Console
Applicatio
¤ 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
Like Java and C++, Visual C# begins its comments with the slash-and-asterisk combination (/*) and ends comments with the asterisk-and-slash combination (*/). However, there are some minor variations. Java and C# have the same comment structure, but C++ is slightly different. In case you want to copy comments from your Java and/or C++ program over to your Visual C# program. Here are examples of comments in Visual C#, Java, and C++.
VISUAL C# |
JAVA |
C++ |
|||
// |
|
// |
|
// |
|
/* |
comment */ |
/* |
comment */ |
/* |
comment */ |
Comments
■ The Class1.cs code appears in the parent window.
Á Delete the comments within the Main method.
‡ Type a decimal value with a two-slash comment immediately afterward.
■ The comment code color turns green to differentiate your comment from other code.
° Type an integer value and a comment by placing /* at the beginning and an */ at the end of the comment.
■ The comment code turns green to differentiate your comment from other code.
Note: When you finish typing in an asterisk and slash, your comment appears in boldface; this signifies that you have a complete comment.
· Save the program as the filename.
55
C#
WRITE YOUR FIRST PROGRAM
Your first program lets you become acquainted with programming in the MDE window, gives you confidence in programming with C#, and provides the
enjoyment of seeing your first program compile. The Hello, world! program is the ubiquitous first program that people write no matter what programming language you write the program in. Visual C# is brand new, so this task shows you how to write a Hello, world! program so you can announce your program, and Visual C#, to the rest of the world.
You can program your Hello, world! program in several different ways, though this task shows you only one of them. You can view all sample Hello, world! programs in the
Visual Studio .NET online help available in the MDE window. See page 18 for more information about using Visual Studio .NET online help.
You can also download the Hello, world! samples directly from the Hello World Tutorial page in Visual Studio .NET if you want to open and run them without having to type in all of the variants yourself. After you compile the program, the result appears in a hello.exe file that you can run from Visual C# or by opening the file in Windows and viewing the output in an MS-DOS, for Windows 9x and ME, or console, for Windows NT or 2000, window.
WRITE YOUR FIRST PROGRAM
Console
Applicatio
Click New Project in the
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
If you are used to programming in C or C++, you will notice some changes in the Visual C# code, including the following:
•The program does not use a global Main method.
•The program does not support methods and variables at the global level, instead containing those elements within type declarations such as class and struct.
•The program does not use :: or -> operators. The former operator does not exist in Visual C# and the latter has limited use.
•The program uses a period (.) in compound names, the most common of which is Console.WriteLine.
•Because the declaration order is not significant in a Visual C# program, forward declarations do not exist.
•The program does not import text by using #include.
•Visual C# eliminates some inter-language barriers; for example, the Console class can be written in another language such as Visual Basic.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
■ The Class1 class appears |
‡ Delete the comments |
· Run the program by |
‚ Save the program as the |
||||||
in the parent window. |
from the Main method. |
pressing the F5 key. |
filename. |
||||||
|
Á Rename Class1 as |
|
° Type the |
■ The Hello, world! text |
|
||||
|
|
|
|||||||
Hello. |
Console.WriteLine |
appears. |
|
||||||
|
|
|
|
|
statement that announces |
|
|
||
|
|
|
|
|
your program to the world. |
|
|
57