- •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 CONSTANT EXPRESSIONS
constant expression describes a snippet of code that Acontains a constant value that the compiler evaluates
when your project compiles. An example of a constant value is x = 5. A constant expression contains 1 of
16 types and 1 of 9 different constructs.
The type of a constant expression includes the following: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, string, any enumeration type, or null. Some of these types may be familiar to you, such as the int type declaring an integer.
These types will be explored in more detail in this chapter, and you can also view all of the types and their associated value ranges in the MDE window online help.
The constructs you can use in a constant expression include literal keywords (null, true, and false), references to other constant expressions in classes and structs, references to members of enumeration types, nested constant expressions, cast expressions (the conversion of an expression into a type), predefined arithmetic operators
(+, *, and /), and the ?: conditional operator that determines whether one or another value is true. You will not know the results from your constant expression until you compile and run your project.
PROGRAM CONSTANT EXPRESSIONS
Console
Applicatio
n
Properties
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 include a constant in another constant expression.
TYPE THIS:
using System; class Zero
{
public const a = 5;
public const b = a + 10; public static void Main()
{
Console.WriteLine(b
}
}
When the compiler checks for constant expressions, it will do so even if the constant expression is nested within a non-constant construct. If the constant returns an overflow, such as a divide by zero error, then the compiler will return a compile-time error for you to resolve.
The only constant expressions that can apply to reference types are string and null because reference types do not contain actual data — only references to that data.
RESULT:
15
■ The Class1.cs code appears in the parent window.
Á Delete the comments within the Main method.
‡ Type the code that specifies the constant expression and outputs the expression using the object name (Class1) and variable (x).
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The constant expressions |
|
appear onscreen. |
|
89
C#
SPECIFY VALUE TYPES
ou cannot create a Visual C# project without value Ytypes. Value types come in two types: struct and
enumeration.
Fourteen other value types exist besides the struct and enum types; Visual C# groups these types into simple types.
Eleven of these twelve simple types are numeric, and the remaining simple value type, bool, is a Boolean value.
These numeric types define the types of numbers that you have specified or you want the user to enter in a field.
Visual C# contains a built-in System namespace that contains all the reference information for predefined types.
The simple types act as aliases for these predefined types that the compiler uses when you compile your project. Visual C# also has two other predefined types, object and string, that are not simple types because they are used with reference types. Unlike reference types, value types cannot contain the null value.
Each value type contains an implicit constructor that tells the compiler to initialize the default value if you do not specify a value. The default values appear in the Default Values Table help page that you can access from online help in the MDE window.
SPECIFY VALUE TYPES
Properties
¤ Click New 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
You can display the actual value type for any C# type using the method GetType().
TYPE THIS: |
RESULT: |
using System;
class Type; Uint64
{
public static void Main()
{
Console.WriteLine(15500L.GetType());
}
}
■ The Class1.cs code appears in the parent window.
Á Delete the comments within the Main method.
‡ Type the code to specify value type variables and output those variables.
° Run the program by |
· Save the program as the |
pressing the F5 key. |
filename. |
■ The values appear |
|
onscreen. |
|
91
