- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
methods. The declaration of a delegate requires the delegate keyword employed in the following manner:
<modifiers>opt delegate <return-type> <identifier> ( <parameters>opt )
where
• <modifiers> is optional, and must be an accessibility level or the keyword new. If unspecified, a delegate is assigned the default accessibility level of the containing declarative scope. Multiple complementary modifiers may be specified.
•<return-type> is the return type for the delegate.
•<identifier> is the unique name to assign to the delegate.
•<parameters> is optional, and indicates the parameters for the delegate.
Delegate parameters are specified in the same manner as method parameters for a method within a class or structure.
A few examples of delegates are given below. Delegates are also used to create events in the Events discussion on page 644. A detailed example using a delegate appears in section 9.2.1 on page 272.
protected delegate int FindIndex(string name);
public delegate void EventHandler(object sender, EventArgs e);
public delegate Photograph ReadDelegate(StreamReader sr);
A.3 LANGUAGE ELEMENTS
This section presents the built-in types, operators, and keywords of C# in tabular form. The tables present a brief description of each item. The following aspects of the C# language are presented:
•Built-in types
•Operators
•Keywords
A.3.1 BUILT-IN TYPES
The following table summarizes the types built into C#. These types, as well as all user-defined types in C#, implicitly inherit from the object class, which also appears in this table. The table provides a short description of each type, along with each type’s default value and the class used to represent the type in the .NET Framework. Within C# source files written for the framework, the type and the .NET class are interchangeable.
C# built-in types
Type |
Description |
Default value |
.NET class |
|
|
|
|
bool |
A boolean value |
false |
System.Boolean |
byte |
An unsigned 8-bit integer |
(byte)0 |
System.Byte |
|
|
|
|
654 |
APPENDIX A C# PRIMER |
C# built-in types (continued)
Type |
Description |
Default value |
.NET class |
char |
A 16-bit Unicode character |
'\0' |
System.Char |
decimal |
A 128-bit decimal value |
0.0m |
System.Decimal |
double |
A 64-bit floating point value |
0.0d |
System.Double |
float |
A 32-bit floating point value |
0.0f |
System.Single |
int |
A 32-bit integer |
0 |
System.Int32 |
long |
A 64-bit integer |
0L |
System.Int64 |
object |
Any object. The ultimate base class of |
null |
System.Object |
|
any type. |
|
|
sbyte |
An 8-bit integer |
(sbyte)0 |
System.SByte |
short |
A 16-bit integer |
0 |
System.Int16 |
string |
A reference type of a collection of char |
null |
System.String |
|
types |
|
|
uint |
An unsigned 32-bit integer |
0u |
System.UInt32 |
ulong |
An unsigned 64-bit integer |
(ulong)0 |
System.UInt64 |
ushort |
An unsigned 32-bit integer |
(ushort)0 |
System.UInt16 |
|
|
|
|
A.3.2 OPERATORS
Many of the operators in C# are taken from C++ and have identical meanings. The following table summarizes the operators available as they relate to the built-in types. Most of these operators may be overridden for user-defined types. Keyword operators such as true, new, and is are not shown in this table. These are summarized in the table of keywords given in the next section.
C# operators
Category |
Operators |
Examples |
|
|
|
Arithmetic |
+ - * / % |
int num = -12; |
|
|
int age = days / 365; |
|
|
int onesPlace = number % 10; |
Logical (boolean and bitwise) |
& | ^ ! ~ && || |
bool isTrue = ! false; |
|
|
int choices = gates & openSet; |
String concatenation |
+ |
string hi = "Hello " + "World!"; |
Increment, decrement |
++ -- |
index ++; |
Shift |
<< >> |
long kilobyte = 1 << 10; |
Relational |
== != > < <= >= |
bool isDigit = (x >= 0) && (x < 10); |
|
|
|
Assignment |
= += -= *= /= %= &= |
int byFives += 5; |
|
|= ^= <<= >>= |
|
Member access |
. |
return myString.ToLower(); |
|
|
|
LANGUAGE ELEMENTS |
655 |
C# operators (continued)
Category |
Operators |
Examples |
Indexing |
[] |
Photograph first = _album[0]; |
Cast |
() |
short num = (short)7; |
|
|
Photograph photo = (Photograph) obj; |
Conditional |
?: |
int size |
|
|
= (list == null) ? 0 : list.Count; |
Delegates |
+ - += -= |
photo.Display += new |
|
|
DisplayHandler(photo_Display); |
Indirection and Address (in |
* -> {} & |
int num = 11; |
unsafe code only) |
|
int* pnum = # |
|
|
|
A.3.3 KEYWORDS
This section presents a complete list of all keywords used by C#, along with a description and example of each keyword. These keywords are reserved words that have special meanings to the C# compiler, and should not normally be used as identifiers in your programs. To use a reserved keyword as an identifier, prefix the string with an atsign ‘@’ character. For example, while class is a reserved keyword, @class is a valid identifier.
Some of these keywords are discussed in detail in section A.2 beginning on page 639. Many of these keywords also appear elsewhere in the book. Sometimes a detailed discussion is provided, and sometimes the keyword just occurs as part of the presented code. The “See also” column in the following table provides a reference to these sections where appropriate:
C# keywords
Keyword |
Description |
Example |
See also |
|
|
|
|
abstract |
Indicates that a class cannot |
// Define an abstract class |
sealed; |
|
be instantiated and is intended |
public abstract class Person |
Menu class in |
|
as a base for other classes. |
{ |
.NET Table 3.1, |
|
// Define abstract members |
||
|
|
page 72 |
|
|
|
public abstract string Address; |
|
|
|
public abstract Point GetHomeCoord(); |
|
|
|
. . . |
|
|
|
} |
|
|
Within an abstract class, |
|
sealed |
|
indicates that a property or |
|
|
|
method has no |
|
|
|
implementation and must be |
|
|
|
overridden in a derived class. |
|
|
as |
Converts an expression to a |
object obj = lstPhotos.SelectedItem; |
is; |
|
given type. On an error, returns |
Photograph photo = obj as Photograph; |
example in |
|
the value null. |
|
section 9.3.4, |
|
|
|
page 300 |
|
|
|
|
656 |
APPENDIX A C# PRIMER |
C# keywords |
(continued) |
|
|
|
|
|
|
Keyword |
Description |
Example |
See also |
|
|
|
|
base |
Represents the base class |
See example for override keyword. |
example in |
|
from within a derived class. |
|
section 5.3.3, |
|
|
|
page 148 |
bool |
Denotes a boolean type, with |
bool result = photo.IsValidImage(); |
true; |
|
possible values true and |
bool isExample = true; |
false; |
|
false. |
|
discussion in |
|
|
|
section 3.4.1, |
|
|
|
page 89 |
break |
Terminates the enclosing loop |
foreach (Photograph p in _album) |
case; |
|
or conditional construct. |
{ |
examples in |
|
Execution resumes after the |
if (p == myPhoto) |
section 6.7.1, |
|
break; |
||
|
terminated construct. |
page 190 and |
|
|
} |
||
|
|
|
section 18.1.2, |
|
|
|
page 607 |
byte |
Denotes an unsigned 8-bit |
char c = 'y'; |
|
|
integer value, with values 0 to |
byte b = Convert.ToByte(c); |
|
|
255. |
|
|
case |
Identifies a possible |
See example for switch keyword. |
default; |
|
expression within a switch |
|
switch; |
|
statement. |
|
discussion in |
|
|
|
section 6.7.1, |
|
|
|
page 190 and |
|
|
|
section 9.2.1, |
|
|
|
page 272 |
catch |
Identifies a type of exception |
See example for try keyword. |
try; finally |
|
to handle in a try-catch |
|
throw; section |
|
statement. |
|
2.3.2 on page |
|
|
|
58 |
char |
Denotes a Unicode 16-bit |
char response = ReadResponse(); |
|
|
character value. |
char yes = 'y', no = 'n'; |
|
checked |
Performs integer overflow |
try |
unchecked; |
|
checking on the given |
{ |
|
|
statement. If an overflow |
y = checked(a/b + c); |
|
|
} |
|
|
|
occurs, an exception is raised. |
|
|
|
catch (System.OverflowException e) |
|
|
|
By default, all integer |
{ |
|
|
expressions are checked. |
. . . |
|
|
|
} |
|
class |
Defines a new data |
See examples for const and override |
struct; |
|
abstraction, or data type, along |
keywords. |
chapter 5 on |
|
with a set of members that |
|
Reusable |
|
interact with this type. Classes |
|
Libraries, |
|
are represented as reference |
|
section 5.1, |
|
types. A class can inherit from |
|
page 127 |
|
at most one other class and |
|
|
|
from multiple interfaces. |
|
|
|
|
|
|
LANGUAGE ELEMENTS |
657 |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
const |
|
Indicates that a field or variable |
public class BookReference |
readonly; |
|
|
cannot be modified. The value |
{ |
example in |
|
|
for a constant must be |
// Must be assigned here |
section 6.6.1, |
|
|
protected int timeout = 30; |
||
|
|
assigned as part of the |
page 182 |
|
|
|
protected const string defaultURL |
||
|
|
declaration. |
= "www.manning.com/eebrown"; |
|
|
|
|
// Assigned here or in constructor |
|
|
|
|
public readonly string bookURL; |
|
|
|
|
BookReference(string name, string url) |
|
|
|
|
{ |
|
|
|
|
if (url == null) |
|
|
|
|
bookURL = defaultURL; |
|
|
|
|
else |
|
|
|
|
bookURL = url; |
|
|
|
|
. . . |
|
|
|
|
} |
|
|
|
|
} |
|
continue |
|
Passes control to the next |
for (int x = 0; |
example in |
|
|
iteration of the enclosing loop. |
x < Contractors.Count; |
section 15.3.2, |
|
|
|
x++) |
page 498, |
|
|
|
{ |
|
|
|
|
listing 15.1 |
|
|
|
|
if (Contractors[x].IsSalaried) |
|
|
|
|
continue; |
|
|
|
|
// Determine hourly pay |
|
|
|
|
} |
|
decimal |
|
Denotes a decimal number |
decimal circumference; |
|
|
|
with up to roughly 28 |
decimal radius = 7m; |
|
|
|
significant digits. Stored as a |
decimal pi = 3.1415; |
|
|
|
circumference = 2m * pi * radius; |
|
|
|
|
128-bit data value. Use the |
|
|
|
|
|
|
|
|
|
suffix m or M to denote a |
|
|
|
|
numeric value as a decimal |
|
|
|
|
type. |
|
|
delegate |
|
Defines a reference type that |
// Define the ReadDelegate delegate |
discussion in |
|
|
encapsulates a method with a |
public delegate Photograph |
section 1.3.1, |
|
|
specific signature. |
ReadDelegate(StreamReader sr); |
page 20; |
|
|
|
||
|
|
|
|
example in |
|
|
|
|
section 9.2.1, |
|
|
|
|
page 272 |
default |
|
In a switch block, identifies the |
See example for switch keyword. |
case; |
|
|
statement to execute if none |
|
switch; |
|
|
of the given constant |
|
discussion in |
|
|
expressions match the given |
|
section 6.7.1, |
|
|
expression. |
|
page 190 and |
|
|
|
|
section 9.2.1, |
|
|
|
|
page 272 |
do |
|
Executes a statement or block |
do |
while; |
|
|
one or more times until a |
{ |
example in |
|
|
specified while expression |
name = reader.ReadLine(); |
section 6.7.1, |
|
|
if (name != null) |
||
|
|
evaluates to false. |
page 190 |
|
|
|
// Make use of the name |
||
|
|
|
} while (name != null); |
|
|
|
|
|
|
658 |
APPENDIX A C# PRIMER |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
double |
|
Denotes a 64-bit floating point |
double circumference; |
float; |
|
|
value. By default, all non- |
double radius = 7d; |
|
|
|
integral numbers are treated as |
double pi = 3.1415; |
|
|
|
values of this type. Use the d |
circumference = 2d * pi * radius; |
|
|
|
|
|
|
|
|
or D suffix to denote a numeric |
|
|
|
|
value as a double type. |
|
|
else |
|
In an if statement, the |
See example for if keyword. |
if; examples |
|
|
statement to execute if the |
|
throughout text |
|
|
expression returns false. |
|
|
enum |
|
Denotes an enumeration, or |
enum WeekDays= { Sun, Mon, Tue, Wed, |
example in |
|
|
enumerated type, consisting of |
Thu, Fri, Say }; |
section 7.2.2, |
|
|
a defined set of constants each |
|
page 199 |
|
|
assigned a value from a given |
|
|
|
|
integral type. |
|
|
event |
|
Defines a handler abstraction |
class Photograph |
delegate; |
|
|
in which to define a set of |
{ |
section 1.3.1, |
|
|
methods that should be |
public event ReadDelegate LoadPhoto; |
page 20 and |
|
|
. . . |
||
|
|
invoked when a specific |
section 3.3, |
|
|
|
} |
||
|
|
incident, or event, occurs. |
|
page 85 |
|
|
Methods are added or |
|
|
|
|
removed to an event with the |
|
|
|
|
+= and -= operators. |
|
|
explicit |
|
Declares that a type |
public static explicit |
implicit; |
|
|
conversion must be invoked |
operator Photograph(string s) |
|
|
|
with a cast. Omitting the cast |
{ |
|
|
|
// code to convert from string |
|
|
|
|
results in a compile-time error. |
|
|
|
|
} |
|
|
extern |
|
Modifies a class member |
class Photograph |
|
|
|
declaration to indicate that the |
{ |
|
|
|
member is implemented |
public extern void Draw(Graphics g); |
|
|
|
. . . |
|
|
|
|
outside the current class file. |
|
|
|
|
} |
|
|
false |
|
As an operator in user-defined |
public static bool |
true; |
|
|
types, defines the meaning of |
operator false(MyType x) |
|
|
|
“false” for instances of that |
{ |
|
|
|
// Return whether MyType is "false" |
|
|
|
|
type. |
|
|
|
|
} |
|
|
|
|
As a literal, the boolean value |
bool isChapter = false; |
true; |
|
|
of false. |
|
discussion in |
|
|
|
|
section 3.4.2, |
|
|
|
|
page 93 |
finally |
|
Indicates a block of code that |
See example for try keyword. |
catch; |
|
|
executes regardless of |
|
try; |
|
|
whether an exception occurs |
|
throw; |
|
|
in the preceding try block. |
|
example in |
|
|
|
|
section 6.6.1, |
|
|
|
|
page 182 |
|
|
|
|
|
LANGUAGE ELEMENTS |
659 |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
fixed |
|
In unsafe code, prevents |
// In unsafe code, pin current photo |
unsafe |
|
|
relocation of a variable by the |
fixed (Photograph photo = CurrentPhoto) |
|
|
|
garbage collector. |
{ |
|
|
|
// Perform unsafe operations |
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
// CurrentPhoto no longer pinned |
|
float |
|
Denotes a 32-bit floating point |
float circumference; |
double |
|
|
value. Use the f or F suffix to |
float radius = 7f; |
|
|
|
denote a numeric value as a |
float pi = 3.1415f; |
|
|
|
circumference = 2f * pi * radius; |
|
|
|
|
float type. |
|
|
|
|
|
|
|
for |
|
Executes a statement or block |
public bool FindPhoto(string name, |
foreach; |
|
|
repeatedly as long as a given |
out int index) |
example in |
|
|
expression evaluates to true. |
{ |
section 10.2.2, |
|
|
for (int x = 0; x < this.Count; x++) |
||
|
|
|
page 328 |
|
|
|
|
{ |
|
|
|
|
if (this[x].Name == name) |
|
|
|
|
{ |
|
|
|
|
index = x; // assign out param |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
foreach |
|
Executes a statement or block |
foreach (Photograph p in CurrentAlbum) |
for; in; |
|
|
using every element in an array |
{ |
example in |
|
|
or collection, if any. |
// Do something with each Photograph |
section 3.4.2, |
|
|
} |
||
|
|
|
page 93 and |
|
|
|
|
|
|
|
|
|
|
section 5.1.1, |
|
|
|
|
page 128 |
goto |
|
Transfers program control |
do |
|
|
|
directly to a labeled statement. |
{ |
|
|
|
Note: The use of this keyword |
// Do something |
|
|
|
if (unable to continue) |
|
|
|
|
is generally discouraged. |
goto CleanUp; |
|
|
|
|
// Do something else |
|
|
|
|
} while ( some expression ); |
|
|
|
|
CleanUp: |
|
|
|
|
f.Close(); |
|
|
|
In a switch statement, |
switch (version) |
|
|
|
transfers control to a given |
{ |
|
|
|
case label or to the default |
case 67: |
|
|
|
photo = Photograph.ReadVer67(s); |
|
|
|
|
label. |
|
|
|
|
goto default; |
|
|
|
|
|
case 77: |
|
|
|
|
// Version 77 specific tasks |
|
|
|
|
goto case 67; |
|
|
|
|
default: |
|
|
|
|
Photograph.ReadGlobalData(s); |
|
|
|
|
Break; |
|
|
|
|
} |
|
|
|
|
|
|
660 |
APPENDIX A C# PRIMER |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
if |
|
A control statement in which a |
if (_album.Count > 0) |
else; examples |
|
|
statement is executed only if a |
DisplayPhotos(_album); |
throughout |
|
|
given expression evaluates to |
else |
book |
|
|
statusBar.Text = "Album is empty"; |
||
|
|
true. |
|
|
|
|
|
|
|
implicit |
|
Declares that a type |
public static implicit |
explicit |
|
|
conversion should be invoked |
operator Photograph(Bitmap img) |
|
|
|
automatically by the compiler |
{ |
|
|
|
// code to convert from Bitmap |
|
|
|
|
as required. |
|
|
|
|
} |
|
|
in |
|
In a foreach block, separates |
See example for foreach keyword. |
foreach |
|
|
the identifier from the |
|
|
|
|
expression. |
|
|
int |
|
Denotes a 32-bit integer value. |
int apprxCircum |
long; |
|
|
Integer values are treated as |
int radius = 7; |
short |
|
|
int by default. Note that there |
int pi = 31415; |
|
|
|
is no implicit conversion from |
apprxCircum = 2 * pi * radius / 10000); |
|
|
|
|
|
|
|
|
floating point values to int. |
|
|
interface |
|
Defines a new data |
interface IBookDisplay |
class; |
|
|
abstraction, or data type, in |
{ |
struct; |
|
|
which all members are |
// Declaration of interface members |
section 5.1 on |
|
|
} |
||
|
|
implicitly abstract. A class or |
page 127 |
|
|
|
|
||
|
|
structure can inherit from |
|
|
|
|
multiple interfaces. |
|
|
internal |
|
Access modifier for types and |
See example for public keyword. |
public; |
|
|
type members that indicates |
|
protected; |
|
|
the identifier is only accessible |
|
private; |
|
|
by objects within the same |
|
see section |
|
|
assembly. |
|
16.4.1, page |
|
|
|
|
543 |
is |
|
Identifies whether a given |
object obj = lstPhotos.SelectedItem; |
as; |
|
|
expression can be converted, |
if (obj is Photograph) |
discussion in |
|
|
or cast, to a given type. |
{ |
section 3.4.1, |
|
|
Photograph photo = (Photograph) obj; |
||
|
|
|
page 89 |
|
|
|
|
. . . |
|
|
|
|
} |
|
lock |
|
Marks a statement block as a |
public void SortPhotos(bool ascending) |
|
|
|
critical section, ensuring that |
{ |
|
|
|
only one thread can execute |
lock (this) |
|
|
|
{ |
|
|
|
|
the statement block at a time. |
|
|
|
|
. . . |
|
|
|
|
|
} |
|
|
|
|
} |
|
long |
|
Denotes a 64-bit integer value. |
long apprxCircum |
int; |
|
|
Use the L suffix to denote an |
long radius = (long)7; |
short |
|
|
integer value as a long type. |
long pi = (long) 314159265; |
|
|
|
apprxCircum |
|
|
|
|
The l suffix may also be used, |
|
|
|
|
= 2L * pi * radius / 100000000L); |
|
|
|
|
but is easily confused with the |
|
|
|
|
number 1 and is not |
|
|
|
|
recommended. |
|
|
|
|
|
|
|
LANGUAGE ELEMENTS |
661 |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
namespace |
|
Declares a scope for organizing |
namespace MyPhotoAlbum |
example in |
|
|
code and naming types and |
{ |
section 5.2.1, |
|
|
members. If no namespace is |
class PhotoAlbum : CollectionBase |
page 134; |
|
|
{ |
||
|
|
defined, an object is part of the |
step in section |
|
|
|
. . . |
||
|
|
unnamed, or global, |
} |
9.1.1, page 265 |
|
|
namespace. |
} |
|
new |
|
As an operator, creates an |
int index = new int(); |
discussion on |
|
|
object and invokes its |
string s; |
page section |
|
|
constructor. Value types are |
Photograph photo = new Photograph(s); |
1.1.3, page 9 |
|
|
|
||
|
|
created in place, while |
s = new string(); |
|
|
|
reference types are created on |
|
|
|
|
the heap. |
|
|
|
|
As a modifier, explicitly hides a |
public MainForm : Form |
override; |
|
|
member inherited from a |
{ |
discussion in |
|
|
derived class. This is typically |
. . . |
section 5.4.2, |
|
|
protected new void OnLoad(EventArgs e) |
||
|
|
used to give a new meaning or |
page 154 |
|
|
|
{ |
||
|
|
purpose to an identifier. |
. . . |
|
|
|
|
} |
|
|
|
|
} |
|
null |
|
Literal that represents an |
Photograph photo = _album.CurrentPhoto; |
examples |
|
|
uninitialized state, often |
if (photo != null) |
throughout text |
|
|
referred to as a null reference. |
{ |
|
|
|
// Do something with photograph |
|
|
|
|
This is the default value for all |
|
|
|
|
} |
|
|
|
|
reference types. |
|
|
object |
|
The base class of all types in |
object o1 = 7; |
.NET Table 5.3 |
|
|
C#. Any value of any type can |
object o2 = new string("hear me roar!"); |
on page 155 |
|
|
be assigned to variables of |
object o3 = _album.CurrentPhoto; |
|
|
|
|
|
|
|
|
type object. |
|
|
operator |
|
Declares the behavior of an |
public static Complex |
explicit; |
|
|
operator when used with a |
operator –(Complex x) |
implicit |
|
|
specific type, such as a class |
{ |
|
|
|
return new Complex(-x.Real, -x.Imgn); |
|
|
|
|
or structure. Three kinds of |
|
|
|
|
} |
|
|
|
|
operators are supported: unary |
public static Complex |
|
|
|
operators, binary operators, |
|
|
|
|
and conversion operators. |
operator +(Complex x, Complex y) |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
return new Complex(x.Real + y.Real, |
|
|
|
|
x.Imgn + y.Imgn); |
|
|
|
|
} |
|
out |
|
Indicates that any changes |
See example for for keyword. |
ref; |
|
|
made to a method parameter |
|
params |
|
|
should be reflected in the |
|
|
|
|
variable when control returns |
|
|
|
|
to the caller. A variable used as |
|
|
|
|
an out method parameter may |
|
|
|
|
be uninitialized. |
|
|
|
|
|
|
|
662 |
APPENDIX A C# PRIMER |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
override |
|
Explicitly replaces a member |
public class CollectionBase |
new; |
|
|
inherited from a derived class. |
{ |
discussion in |
|
|
This is typically used to provide |
. . . |
section 5.4.2, |
|
|
public virtual void Clear() |
||
|
|
a more appropriate |
page 154 |
|
|
|
{ |
||
|
|
implementation of an inherited |
// Base implementation of Clear |
|
|
|
member in the current type. |
} |
|
|
|
|
} |
|
|
|
|
public class PhotoAlbum : CollectionBase |
|
|
|
|
{ |
|
|
|
|
. . . |
|
|
|
|
public override void Clear() |
|
|
|
|
{ |
|
|
|
|
// Override implementation of Clear |
|
|
|
|
base.Clear(); |
|
|
|
|
} |
|
|
|
|
public static void Main() |
|
|
|
|
{ |
|
|
|
|
CollectionBase c = new PhotoAlbum(); |
|
|
|
|
// invokes PhotoAlbum.Clear |
|
|
|
|
c.Clear(); |
|
|
|
|
} |
|
|
|
|
} |
|
params |
|
Indicates that a method will |
public void AddRange |
out; |
|
|
receive a set of parameters. |
(params Photograph[] photos) |
ref |
|
|
This can occur only once and at |
{ |
|
|
|
the end of the list of |
foreach (Photograph p in photos) |
|
|
|
{ |
|
|
|
|
parameters. |
_album.Add(p); |
|
|
|
|
} |
|
|
|
|
} |
|
private |
|
Access modifier for types and |
public class PhotoAlbum : CollectionBase |
internal; |
|
|
type members that indicates |
{ |
section 1.2.2, |
|
|
the object or member is |
// only available within this class |
page 16 and |
|
|
private int _defaultPhotoIndex; |
||
|
|
accessible only to the type in |
section 9.1.1, |
|
|
|
|
||
|
|
which it is defined. |
// Only available in this assembly |
page 265 |
|
|
|
internal bool IsDisplayed |
|
|
|
|
{ |
|
|
|
|
. . . |
|
|
|
|
} |
|
|
|
|
// available to any derived class |
|
|
|
|
protected void TurnPage() |
|
|
|
|
{ |
|
|
|
|
. . . |
|
|
|
|
} |
|
|
|
|
// available to any type |
|
|
|
|
public Photogram CurrentPhoto |
|
|
|
|
{ |
|
|
|
|
. . . |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
LANGUAGE ELEMENTS |
663 |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
protected |
|
Access modifier for types and |
|
section 1.2.2, |
|
|
type members that indicates |
|
page 16 and |
|
|
the object or member is only |
|
section 9.1.1, |
|
|
accessible by the containing |
|
page 265 |
|
|
type or by types derived from |
|
|
|
|
the containing type. |
|
|
public |
|
Access modifier for types and |
|
section 1.2.2, |
|
|
type members that indicates |
|
page 16 and |
|
|
the object or member is |
|
section 9.1.1, |
|
|
accessible by any type. |
|
page 265 |
readonly |
|
Indicates that a field cannot be |
See example for const keyword. |
const |
|
|
assigned except in the |
|
|
|
|
declaration of the field or the |
|
|
|
|
constructor of the containing |
|
|
|
|
type. |
|
|
ref |
|
Indicates that any changes |
// Locate photo after given index |
out; |
|
|
made to a method parameter |
public bool FindPhotoAfter |
params; |
|
|
should be reflected in the |
(string name, ref int index) |
section 18.1.2, |
|
|
{ |
||
|
|
variable when control returns |
page 607 |
|
|
|
. . . |
||
|
|
to the caller. Unlike the out |
} |
|
|
|
keyword, a variable used as a |
|
|
|
|
ref method parameter must |
|
|
|
|
be initialized. |
|
|
return |
|
Terminates execution of the |
See example for for keyword. |
examples |
|
|
containing method and passes |
|
throughout text |
|
|
control and the result of the |
|
|
|
|
method back to the caller. |
|
|
sbyte |
|
Denotes a signed 8-bit integer |
sbyte sb = 'y'; |
byte |
|
|
value from –128 to 127. An |
sbyte sb = (sbyte)5; e |
|
|
|
explicit cast is required to |
|
|
|
|
convert an integer value to a |
|
|
|
|
sbyte type. |
|
|
sealed |
|
Indicates that a class cannot |
public sealed class |
sealed; |
|
|
be inherited. A sealed class |
SecurePerson : Person |
Application |
|
|
cannot also be abstract. |
{ |
class on |
|
|
. . . |
||
|
|
Note that struct types are |
page 12 |
|
|
|
} |
||
|
|
implicitly sealed. |
|
|
short |
|
Denotes a 16-bit integer value |
short apprxCircum |
int; |
|
|
from –32,768 to 32,768. An |
short radius = (short)7; |
long |
|
|
explicit cast is required to |
short pi = (short) 314; |
|
|
|
apprxCircum |
|
|
|
|
convert an integer value to a |
|
|
|
|
= (short)(2 * pi * radius / 100); |
|
|
|
|
short type. |
|
|
sizeof |
|
Determines the size in bytes of |
int size1 = sizeof(long); |
|
|
|
a value type. |
int size2 = sizeof(Rectangle); |
|
|
|
|
int size3 = sizeof(Complex); |
|
|
|
|
|
|
664 |
APPENDIX A C# PRIMER |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
stackalloc |
|
In unsafe code, allocates a |
public unsafe void QuickSort() |
unsafe |
|
|
block of memory on the stack |
{ |
|
|
|
and returns a pointer to this |
Photograph* photos |
|
|
|
= stackalloc Photograph[Count]; |
|
|
|
|
block. This memory is not |
|
|
|
|
|
|
|
|
|
subject to garbage collection |
// Sort album contents |
|
|
|
and is valid only within the |
// using local memory |
|
|
|
method in which it is defined. |
} |
|
|
|
|
|
|
static |
|
Declares a member that is |
private string _defaultDir |
example in |
|
|
associated with the type itself |
= @"C:\My Documents\Albums"; |
section 5.4.1, |
|
|
rather than with each instance |
public static string DefaultAlbumDir |
page 151 |
|
|
of that type. |
|
|
|
|
{ |
|
|
|
|
|
get { return _defaultDir; } |
|
|
|
|
set { _defaultDir = value; } |
|
|
|
|
} |
|
string |
|
Object representing a set of |
string s = null; |
discussion in |
|
|
Unicode characters. While |
string defaultAlbum = "myAlbum"; |
section 5.4.2, |
|
|
string is a reference type, the |
string _defaultDir |
page 154 |
|
|
= @"C:\My Documents\Albums"; |
||
|
|
equality operators == and != |
|
|
|
|
|
|
|
|
|
are defined to compare values |
|
|
|
|
rather than references. |
|
|
struct |
|
Defines a new data |
struct Complex |
class; |
|
|
abstraction, or data type, along |
{ |
discussion in |
|
|
with a set of members that |
double real; |
section 1.1.3, |
|
|
double imaginary; |
||
|
|
interact with this type. |
page 9 |
|
|
|
} |
||
|
|
Structures are represented as |
|
|
|
|
value types, and are implicitly |
|
|
|
|
sealed. |
|
|
switch |
|
Executes one of a given set of |
switch (version) |
case; |
|
|
statements based on the |
{ |
default; |
|
|
constant value of a given |
case 67: |
discussion in |
|
|
photo = Photograph.ReadVer67(s); |
||
|
|
expression. If a match for the |
section 6.7.1, |
|
|
|
break; |
||
|
|
current value is not found, then |
. . . |
page 190 and |
|
|
a default statement can |
default: |
section 9.2.1, |
|
|
optionally be executed. |
page 272 |
|
|
|
throw ApplicationException( |
||
|
|
|
|
|
|
|
|
"Unrecognized album version"); |
|
|
|
|
} |
|
this |
|
Represents the current |
See example for for keyword. |
example in |
|
|
instance for which a method is |
|
section 1.1.2, |
|
|
called. Static member |
|
page 8 |
|
|
functions cannot employ the |
|
|
|
|
this keyword. |
|
|
throw |
|
Raises a new exception, or re- |
See example for switch keyword. |
try; |
|
|
raises a caught exception. |
|
catch; |
|
|
|
|
section 2.3.2, |
|
|
|
|
page 58) |
|
|
|
|
|
LANGUAGE ELEMENTS |
665 |
C# keywords |
(continued) |
|
|
|
|
|
|
|
|
Keyword |
|
Description |
Example |
See also |
|
|
|
|
|
true |
|
As in operator in user-defined |
public static bool |
false |
|
|
types, defines the meaning of |
operator true(MyType x) |
|
|
|
“true” for instances of that |
{ |
|
|
|
// Return whether MyType is "true" |
|
|
|
|
type. |
|
|
|
|
} |
|
|
|
|
As a literal, the boolean value |
bool isAppendix = true; |
false; |
|
|
of true. |
|
discussion in |
|
|
|
|
section 3.4.2, |
|
|
|
|
page 93 |
try |
|
Begins a block in which |
// Open a file |
catch |
|
|
exceptions may be handled, |
FileStream fs = new FileStream(...); |
finally |
|
|
depending on the attached |
try |
throw |
|
|
catch clauses. |
section 2.3.2, |
|
|
|
{ |
||
|
|
|
// Do something with open file |
page 58 |
|
|
|
} |
|
|
|
|
catch (IOException ex) |
|
|
|
|
{ |
|
|
|
|
// Handle caught exception |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
fs.Close(); // ensure file closure |
|
|
|
|
} |
|
typeof |
|
Obtains the System.Type |
Type t = typeof(Photograph); |
code in section |
|
|
object for a given type. Use the |
|
10.5.3, page |
|
|
Object.GetType method to |
|
348 |
|
|
obtain the type instance for an |
|
|
|
|
expression. |
|
|
uint |
|
Denotes an unsigned 32-bit |
uint apprxCircum |
ulong; |
|
|
integer value. Use the u or U |
uint radius = 7u, pi = 314159; |
ushor |
|
|
suffix to denote an integer |
apprxCircum |
|
|
|
= 2u * pi * radius / 100000u; |
|
|
|
|
value as a uint type. |
|
|
|
|
|
|
|
ulong |
|
Denotes an unsigned 64-bit |
ulong apprxCircum |
ulong; |
|
|
integer value. When using the |
ulong radius = 7L; |
ushort |
|
|
L suffix to denote a long |
ulong pi = 31415926535 |
|
|
|
apprxCircum |
|
|
|
|
integer or the U suffix to |
= 2 * pi * radius / 10000000000L); |
|
|
|
denote an unsigned integer, |
|
|
|
|
the value is considered ulong if |
|
|
|
|
it is beyond the range of the |
|
|
|
|
long or uint type, |
|
|
|
|
respectively. |
|
|
unchecked |
|
Suppresses integer overflow |
long bigPrime = 9876543211; |
checked |
|
|
checking on the given |
long notSoBigNum |
|
|
|
statement. If an overflow |
= unchecked(bigPrime * bigPrime); |
|
|
|
|
|
|
|
|
occurs, the result is truncated. |
|
|
|
|
By default, all integer |
|
|
|
|
expressions are checked. |
|
|
|
|
|
|
|
666 |
APPENDIX A C# PRIMER |