
- •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 THE BOOLEAN TYPE
The Boolean type lets you determine if a variable or expression meets one of two criteria: True or False. Using the Boolean type is a good way to determine
how your program functions depending on the values stored in one or more variables in your project.
The Boolean type uses the keyword bool, which is an alias of the System.Boolean type in Visual Studio .NET. You can use the System.Boolean type name as opposed to bool if you wish, but the functionality of the type name and the alias is exactly the same.
You can assign a Boolean value (that is, True or False) or a range of values to a bool keyword. For example, you can tell the bool keyword to check to see if the bool value is True where x > 5 and x < 10. If the value is between 6 and
9, the value will be true, and your project will determine what code block to execute next.
The default value of the Boolean type is False. Therefore, if you enter a bool statement and enter neither the True nor False variables in the statement, Visual C# automatically checks to see if the value in the bool statement is False.
PROGRAM THE BOOLEAN TYPE
Console
Applicatio
Properties
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Click New Project. |
■ The New Project window |
|
|
|
|
|
|||
|
|
|
|
|
|||||
› Type a name for the file. |
|||||||||
|
appears. |
|
ˇ Click OK. |
||||||
|
|
‹ Click the Console |
|
||||||
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|||
|
Application icon in the |
|
|
|
|
|
|||
|
Templates pane. |
|
|
|
|
|

WORKING WITH TYPES AND INTERFACES 5
You can determine whether a particular value meets a certain condition (for example, whether a value is greater than zero) by using Boolean types as the controlling expressions in if and for statements.
TYPE THIS:
using System; class Boolean;
{
int x = 4;
public static void Main()
{
if (x!>= 0)
{
Console.WriteLine("The value of x is greater than zero.");
}
}
}
Unlike C++, which lets you convert a Boolean type to an integer type, Visual C# does not allow any Boolean type conversion.
C++ lets you convert the false state to zero and the true state to a non-zero value. If you want to know if a variable is equal to zero or not, you have to create an if statement that checks if a variable is zero or not.
RESULT:
The value of x is greater than zero.
■ The Class1 appears in window.
Á Delete the within the

C#
DECLARE REFERENCE TYPES
isual C# includes three reference type keywords:
Vclass, interface, and delegate. These keywords declare reference types, but they are not reference
types in and of themselves. Visual C# includes two built-in reference types: object and string. These reference types act as keywords and also the declaration of a reference type in code.
You can assign values of any type to the variables that you include in the object statement. When you convert reference types to value types and vice versa, you do so by declaring those types within the object type before you convert.
DECLARE REFERENCE TYPES
The string type lets you define strings of Unicode characters that can include words, numbers, or any Unicode character. The string can be enclosed in two forms: quotation marks and quotation marks preceded by the @ symbol. The difference the @ symbol makes is that an escape sequence — the backward slash indicates a Unicode character number — is not processed. This makes it easier to enter a filename with all of its directory information that makes use of backward slashes.
The string type acts like a value type in that you can use equality operators for comparing strings and you can use other operators for combining and accessing string characters.
Console
Applicatio
Properties
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Click New Project. |
■ The New Project window |
|
|
|
|
|
|||
|
|
|
|
|
|||||
› Type a name for the file. |
|||||||||
|
appears. |
|
ˇ Click OK. |
||||||
|
|
‹ Click the Console |
|
||||||
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|||
|
Application icon in the |
|
|
|
|
|
|||
|
Templates pane. |
|
|
|
|
|

WORKING WITH TYPES AND INTERFACES 5
If you want to determine if two strings are the same, such as a user-entered password matching the stored password, you can use the equality (==) and inequality (!=) operators for testing whether two strings are the same as you would with two values in a value type declaration.
TYPE THIS:
using System; class EqualityTest;
{
int x = 4; int y = 5
public static void Main()
{
if (x != 0)
{
Console.WriteLine("The value of x is greater than zero.");
}
if (x == 0)
{
Console.WriteLine("The value of x is zero.");
}
}
}
RESULT:
The value of x is greater than zero.
■
Á