- •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 NUMERIC TYPES
umeric types let you specify the type of number you Nassign to a variable. By assigning numbers to
variables, you can perform different calculations. Three different categories of types comprise the numeric types: integral, floating-point, and decimal.
The two most common numeric types are integral and decimal because we use those two number types most often. The integral type category has the most number of types because Visual C# categorizes integer types by the range of the integer. In one case, the char type, the integer is not a number at all.
Visual C# divides the integer ranges into four main groups: byte, short, int, and long. Of these four groups, you can specify whether the integer type is signed or unsigned. A signed integer type contains negative numbers in its
range and an unsigned integer contains a number range that starts with 0.
The number of digits in each integer group provides the most obvious information about the differences between the four groups. The byte group contains numbers up to three digits, the short type contains numbers up to five digits, the int type contains numbers up to ten digits, and the long type contains numbers up to 19 digits.
The char type is an integer that represents a Unicode character set value that ranges from 0 to 65535.
PROGRAM NUMERIC 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 determine whether an integer type is signed or unsigned by adding an s or a u before the type name. Only the byte type requires an s in front (thus sbyte) so you can signify the byte as signed. The other three types — short, int, and long — require you to precede those type names so you can signify those types as unsigned.
The Unicode character set is a worldwide standard set that applies numbers to different characters for most written languages throughout the world. When you declare a char variable, you can declare the variable as a letter or with the Unicode number that applies to that letter. For example, you can include a char line with char Letter = 'X';.
You can also provide the Unicode equivalent in place of X, as in char Letter = '\u0058';.
When you enter a Unicode character number you must include the Unicode number in single quotes, precede the number with a backslash and u, and also ensure that the Unicode number has four digits.
You can convert a char value to several other integer types including ushort, int, uint, long, and ulong. However, you cannot convert other integer types (or any other numeric type) to the char type.
■ The Class1.cs code appears in the parent window.
Á Delete the comments within the Main method.
‡ Type the code that adds two integral expressions and outputs the combined expression.
° Run the program by |
■ The combined expression |
pressing the F5 key. |
appears onscreen. |
CONTINUED
93
C#
PROGRAM NUMERIC TYPES
loating and decimal types make up the two other Fcategories of numeric types that Visual C# supports.
Visual C# offers two different floating point types: float and double. You can use the float type for very large numbers — the float range is from ±1.5 x 10 –45 to
±3.4 x 1038, and the float type rounds off numbers to seven digits. You must denote a float type by using the suffix f after the floating point value.
If you need even larger numbers, the double type gives you a far greater range — ±5.0 x 10 –324 to ±1.7 x 10308 — and it rounds off numbers to 15 or 16 digits depending on the number. Double numbers require no suffix after the value.
The decimal type does not give you the range of the floating point type — the decimal type ranges from 1.0 x
10 –28 to 7.9 x 1028 — but it does give you greater precision by rounding off numbers to 28 or 29 digits depending on the number.
You must denote a decimal type by using the suffix m after the decimal value. If you do not use the f and m suffixes for floating-point and decimal values, the value will be treated as a double-value, and your project cannot compile.
PROGRAM NUMERIC TYPES (CONTINUED)
‚ Run the program by pressing the F5 key.
■ The integer and float values appear onscreen.
WORKING WITH TYPES AND INTERFACES 5
If you want to enter a Unicode character, you can do so in C#. The Unicode character set is a worldwide standard set that applies numbers to different characters for most written languages throughout the world. When you declare a char variable, you can declare the variable as a letter or with the Unicode number that applies to that letter.
|
TYPE THIS: |
|
RESULT: |
|
|
||
|
using System; |
|
X |
|
class Character; |
|
|
|
|
|
|
|
{ |
|
X |
|
char Letter1 = ‘X’; |
|
|
|
char Letter2 = ‘\u0058’ |
|
|
|
public static void Main() |
|
|
|
{ |
|
|
|
Console.WriteLine(Letter1); |
|
|
|
Console.WriteLine(Letter2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
You can mix integral and floating point types in one expression. When you mix types, the integral types will convert into floating point types. However, you cannot mix decimal types with integral or floating point types. Make sure to denote the decimal with the m suffix otherwise your project will not compile.
— Add code to |
the |
output character |
|
95