- •Contents at a Glance
- •Table of Contents
- •Acknowledgments
- •Introduction
- •Who This Book Is For
- •Finding Your Best Starting Point in This Book
- •System Requirements
- •Conventions and Features in This Book
- •Conventions
- •Other Features
- •Code Samples
- •Installing the Code Samples
- •Using the Code Samples
- •Support for This Book
- •Questions and Comments
- •Beginning Programming with the Visual Studio 2008 Environment
- •Writing Your First Program
- •Using Namespaces
- •Creating a Graphical Application
- •Chapter 1 Quick Reference
- •Understanding Statements
- •Identifying Keywords
- •Using Variables
- •Naming Variables
- •Declaring Variables
- •Working with Primitive Data Types
- •Displaying Primitive Data Type Values
- •Using Arithmetic Operators
- •Operators and Types
- •Examining Arithmetic Operators
- •Controlling Precedence
- •Using Associativity to Evaluate Expressions
- •Associativity and the Assignment Operator
- •Incrementing and Decrementing Variables
- •Declaring Implicitly Typed Local Variables
- •Chapter 2 Quick Reference
- •Declaring Methods
- •Specifying the Method Declaration Syntax
- •Writing return Statements
- •Calling Methods
- •Specifying the Method Call Syntax
- •Applying Scope
- •Overloading Methods
- •Writing Methods
- •Chapter 3 Quick Reference
- •Declaring Boolean Variables
- •Using Boolean Operators
- •Understanding Equality and Relational Operators
- •Understanding Conditional Logical Operators
- •Summarizing Operator Precedence and Associativity
- •Using if Statements to Make Decisions
- •Understanding if Statement Syntax
- •Using Blocks to Group Statements
- •Cascading if Statements
- •Using switch Statements
- •Understanding switch Statement Syntax
- •Following the switch Statement Rules
- •Chapter 4 Quick Reference
- •Using Compound Assignment Operators
- •Writing while Statements
- •Writing for Statements
- •Understanding for Statement Scope
- •Writing do Statements
- •Chapter 5 Quick Reference
- •Coping with Errors
- •Trying Code and Catching Exceptions
- •Handling an Exception
- •Using Multiple catch Handlers
- •Catching Multiple Exceptions
- •Using Checked and Unchecked Integer Arithmetic
- •Writing Checked Statements
- •Writing Checked Expressions
- •Throwing Exceptions
- •Chapter 6 Quick Reference
- •The Purpose of Encapsulation
- •Controlling Accessibility
- •Working with Constructors
- •Overloading Constructors
- •Understanding static Methods and Data
- •Creating a Shared Field
- •Creating a static Field by Using the const Keyword
- •Chapter 7 Quick Reference
- •Copying Value Type Variables and Classes
- •Understanding Null Values and Nullable Types
- •Using Nullable Types
- •Understanding the Properties of Nullable Types
- •Using ref and out Parameters
- •Creating ref Parameters
- •Creating out Parameters
- •How Computer Memory Is Organized
- •Using the Stack and the Heap
- •The System.Object Class
- •Boxing
- •Unboxing
- •Casting Data Safely
- •The is Operator
- •The as Operator
- •Chapter 8 Quick Reference
- •Declaring an Enumeration
- •Using an Enumeration
- •Choosing Enumeration Literal Values
- •Choosing an Enumeration’s Underlying Type
- •Working with Enumerations
- •Working with Structures
- •Declaring a Structure
- •Understanding Structure and Class Differences
- •Declaring Structure Variables
- •Understanding Structure Initialization
- •Copying Structure Variables
- •Chapter 9 Quick Reference
- •What Is an Array?
- •Declaring Array Variables
- •Creating an Array Instance
- •Initializing Array Variables
- •Creating an Implicitly Typed Array
- •Accessing an Individual Array Element
- •Iterating Through an Array
- •Copying Arrays
- •What Are Collection Classes?
- •The ArrayList Collection Class
- •The Queue Collection Class
- •The Stack Collection Class
- •The Hashtable Collection Class
- •The SortedList Collection Class
- •Using Collection Initializers
- •Comparing Arrays and Collections
- •Using Collection Classes to Play Cards
- •Chapter 10 Quick Reference
- •Using Array Arguments
- •Declaring a params Array
- •Using params object[ ]
- •Using a params Array
- •Chapter 11 Quick Reference
- •What Is Inheritance?
- •Using Inheritance
- •Base Classes and Derived Classes
- •Calling Base Class Constructors
- •Assigning Classes
- •Declaring new Methods
- •Declaring Virtual Methods
- •Declaring override Methods
- •Understanding protected Access
- •Understanding Extension Methods
- •Chapter 12 Quick Reference
- •Understanding Interfaces
- •Interface Syntax
- •Interface Restrictions
- •Implementing an Interface
- •Referencing a Class Through Its Interface
- •Working with Multiple Interfaces
- •Abstract Classes
- •Abstract Methods
- •Sealed Classes
- •Sealed Methods
- •Implementing an Extensible Framework
- •Summarizing Keyword Combinations
- •Chapter 13 Quick Reference
- •The Life and Times of an Object
- •Writing Destructors
- •Why Use the Garbage Collector?
- •How Does the Garbage Collector Work?
- •Recommendations
- •Resource Management
- •Disposal Methods
- •Exception-Safe Disposal
- •The using Statement
- •Calling the Dispose Method from a Destructor
- •Making Code Exception-Safe
- •Chapter 14 Quick Reference
- •Implementing Encapsulation by Using Methods
- •What Are Properties?
- •Using Properties
- •Read-Only Properties
- •Write-Only Properties
- •Property Accessibility
- •Understanding the Property Restrictions
- •Declaring Interface Properties
- •Using Properties in a Windows Application
- •Generating Automatic Properties
- •Initializing Objects by Using Properties
- •Chapter 15 Quick Reference
- •What Is an Indexer?
- •An Example That Doesn’t Use Indexers
- •The Same Example Using Indexers
- •Understanding Indexer Accessors
- •Comparing Indexers and Arrays
- •Indexers in Interfaces
- •Using Indexers in a Windows Application
- •Chapter 16 Quick Reference
- •Declaring and Using Delegates
- •The Automated Factory Scenario
- •Implementing the Factory Without Using Delegates
- •Implementing the Factory by Using a Delegate
- •Using Delegates
- •Lambda Expressions and Delegates
- •Creating a Method Adapter
- •Using a Lambda Expression as an Adapter
- •The Form of Lambda Expressions
- •Declaring an Event
- •Subscribing to an Event
- •Unsubscribing from an Event
- •Raising an Event
- •Understanding WPF User Interface Events
- •Using Events
- •Chapter 17 Quick Reference
- •The Problem with objects
- •The Generics Solution
- •Generics vs. Generalized Classes
- •Generics and Constraints
- •Creating a Generic Class
- •The Theory of Binary Trees
- •Building a Binary Tree Class by Using Generics
- •Creating a Generic Method
- •Chapter 18 Quick Reference
- •Enumerating the Elements in a Collection
- •Manually Implementing an Enumerator
- •Implementing the IEnumerable Interface
- •Implementing an Enumerator by Using an Iterator
- •A Simple Iterator
- •Chapter 19 Quick Reference
- •What Is Language Integrated Query (LINQ)?
- •Using LINQ in a C# Application
- •Selecting Data
- •Filtering Data
- •Ordering, Grouping, and Aggregating Data
- •Joining Data
- •Using Query Operators
- •Querying Data in Tree<TItem> Objects
- •LINQ and Deferred Evaluation
- •Chapter 20 Quick Reference
- •Understanding Operators
- •Operator Constraints
- •Overloaded Operators
- •Creating Symmetric Operators
- •Understanding Compound Assignment
- •Declaring Increment and Decrement Operators
- •Implementing an Operator
- •Understanding Conversion Operators
- •Providing Built-In Conversions
- •Creating Symmetric Operators, Revisited
- •Adding an Implicit Conversion Operator
- •Chapter 21 Quick Reference
- •Creating a WPF Application
- •Creating a Windows Presentation Foundation Application
- •Adding Controls to the Form
- •Using WPF Controls
- •Changing Properties Dynamically
- •Handling Events in a WPF Form
- •Processing Events in Windows Forms
- •Chapter 22 Quick Reference
- •Menu Guidelines and Style
- •Menus and Menu Events
- •Creating a Menu
- •Handling Menu Events
- •Shortcut Menus
- •Creating Shortcut Menus
- •Windows Common Dialog Boxes
- •Using the SaveFileDialog Class
- •Chapter 23 Quick Reference
- •Validating Data
- •Strategies for Validating User Input
- •An Example—Customer Information Maintenance
- •Performing Validation by Using Data Binding
- •Changing the Point at Which Validation Occurs
- •Chapter 24 Quick Reference
- •Querying a Database by Using ADO.NET
- •The Northwind Database
- •Creating the Database
- •Using ADO.NET to Query Order Information
- •Querying a Database by Using DLINQ
- •Creating and Running a DLINQ Query
- •Deferred and Immediate Fetching
- •Joining Tables and Creating Relationships
- •Deferred and Immediate Fetching Revisited
- •Using DLINQ to Query Order Information
- •Chapter 25 Quick Reference
- •Using Data Binding with DLINQ
- •Using DLINQ to Modify Data
- •Updating Existing Data
- •Adding and Deleting Data
- •Chapter 26 Quick Reference
- •Understanding the Internet as an Infrastructure
- •Understanding Web Server Requests and Responses
- •Managing State
- •Understanding ASP.NET
- •Creating Web Applications with ASP.NET
- •Building an ASP.NET Application
- •Understanding Server Controls
- •Creating and Using a Theme
- •Chapter 27 Quick Reference
- •Comparing Server and Client Validations
- •Validating Data at the Web Server
- •Validating Data in the Web Browser
- •Implementing Client Validation
- •Chapter 28 Quick Reference
- •Managing Security
- •Understanding Forms-Based Security
- •Implementing Forms-Based Security
- •Querying and Displaying Data
- •Understanding the Web Forms GridView Control
- •Displaying Customer and Order History Information
- •Paging Data
- •Editing Data
- •Updating Rows Through a GridView Control
- •Navigating Between Forms
- •Chapter 29 Quick Reference
- •What Is a Web Service?
- •The Role of SOAP
- •What Is the Web Services Description Language?
- •Nonfunctional Requirements of Web Services
- •The Role of Windows Communication Foundation
- •Building a Web Service
- •Creating the ProductsService Web Service
- •Web Services, Clients, and Proxies
- •Talking SOAP: The Easy Way
- •Consuming the ProductsService Web Service
- •Chapter 30 Quick Reference
176 |
Part II Understanding the C# Language |
In a class, you can initialize instance fields at their point of declaration. In a structure, you cannot. The following example would compile if Time were a class, but because Time is a structure, it causes a compile-time error:
struct Time
{
...
private int hours = 0; // compile-time error private int minutes;
private int seconds;
}
The following table summarizes the main differences between a structure and a class.
Question |
Structure |
Class |
Is this a value type or a reference type?
A structure is a value |
A class is a reference type. |
type. |
|
Do instances live on the stack or the heap? |
Structure instances |
Class instances are called |
|
are called values and |
objects and live on the |
|
live on the stack. |
heap. |
|
|
|
Can you declare a default constructor? |
No |
Yes |
|
|
|
If you declare your own constructor, will |
Yes |
No |
the compiler still generate the default |
|
|
constructor? |
|
|
|
|
|
If you don’t initialize a field in your own |
No |
Yes |
constructor, will the compiler automati- |
|
|
cally initialize it for you? |
|
|
Are you allowed to initialize instance fields |
No |
Yes |
at their point of declaration? |
|
|
|
|
|
There are other differences between classes and structures concerning inheritance. These differences are covered in Chapter 12, “Working with Inheritance.” Now that you know how to declare structures, the next step is to use them to create values.
Declaring Structure Variables
After you have defined a structure type, you can use it in exactly the same way as any other type. For example, if you have defined the Time structure, you can create variables, fields, and parameters of type Time, as shown in this example:
struct Time
{
...
private int hours, minutes, seconds;
}
Chapter 9 Creating Value Types with Enumerations and Structs |
177 |
class Example
{
public void Method(Time parameter)
{
Time localVariable;
...
}
private Time currentTime;
}
Note You can create a nullable version of a structure variable by using the ? modifier. You can then assign the null value to the variable:
Time? currentTime = null;
Understanding Structure Initialization
Earlier in this chapter, you saw how the fields in a structure are initialized by using a constructor. However, because structures are value types, you can create structure variables without calling a constructor, as shown in the following example:
Time now;
In this example, the variable is created but its fields are left in their uninitialized state. Any attempt to access the values in these fields will result in a compiler error. The following graphic depicts the state of the fields in the now variable:
If you call a constructor, the various rules of structure constructors described earlier guarantee that all the fields in the structure will be initialized:
Time now = new Time();
178 Part II Understanding the C# Language
This time, the default constructor initializes the fields in the structure, as shown in the following graphic:
Note that in both cases, the Time variable is created on the stack.
If you’ve written your own structure constructor, you can also use that to initialize a structure variable. As explained earlier in this chapter, a structure constructor must always explicitly initialize all its fields. For example:
struct Time
{
public Time(int hh, int mm)
{
hours = hh; minutes = mm; seconds = 0;
}
...
private int hours, minutes, seconds;
}
The following example initializes now by calling a user-defined constructor:
Time now = new Time(12, 30);
The following graphic shows the effect of this example:
Chapter 9 Creating Value Types with Enumerations and Structs |
179 |
Copying Structure Variables
You’re allowed to initialize or assign one structure variable to another structure variable, but only if the structure variable on the right side is completely initialized (that is, if all its fields are initialized). The following example compiles because now is fully initialized. (The graphic shows the results of performing the assignment.)
Time now = new Time(12, 30);
Time copy = now;
The following example fails to compile because now is not initialized:
Time now;
Time copy = now; // compile-time error: now has not been assigned
When you copy a structure variable, each field on the left side is set directly from the corresponding field on the right side. This copying is done as a fast, single operation that copies the contents of the entire structure and that never throws an exception. Compare this behavior with the equivalent action if Time were a class, in which case both variables (now and copy) would end up referencing the same object on the heap.
Note C++ programmers should note that this copy behavior cannot be customized.
It’s time to put this knowledge into practice. In the following exercise, you will create and use a structure to represent a date.
180 |
Part II Understanding the C# Language |
Create and use a structure type
1.In the StructsAndEnums project, display the Date.cs file in the Code and Text Editor window.
2.Add a structure named Date inside the StructsAndEnums namespace.
This structure should contain three private fields: one named year of type int, one named month of type Month (using the enumeration you created in the preceding exercise), and one named day of type int. The Date structure should look exactly as follows:
struct Date
{
private int year; private Month month; private int day;
}
Consider the default constructor that the compiler will generate for Date. This constructor will set the year to 0, the month to 0 (the value of January), and the day to 0. The year value 0 is not valid (there was no year 0), and the day value 0 is also not valid (each month starts on day 1). One way to fix this problem is to translate the year and
day values by implementing the Date structure so that when the year field holds the value Y, this value represents the year Y + 1900 (you can pick a different century if you prefer), and when the day field holds the value D, this value represents the day D + 1. The default constructor will then set the three fields to values that represent the date 1 January 1900.
3.Add a public constructor to the Date structure. This constructor should take three parameters: an int named ccyy for the year, a Month named mm for the month, and an int named dd for the day. Use these three parameters to initialize the corresponding fields. A year field of Y represents the year Y + 1900, so you need to initialize the year field to the value ccyy – 1900. A day field of D represents the day D + 1, so you need to initialize the day field to the value dd – 1.
The Date structure should now look like this (the constructor is shown in bold):
struct Date
{
public Date(int ccyy, Month mm, int dd)
{
this.year = ccyy - 1900; this.month = mm; this.day = dd - 1;
}
private int year; private Month month; private int day;
}
Chapter 9 Creating Value Types with Enumerations and Structs |
181 |
4.Add a public method named ToString to the Date structure after the constructor. This
method takes no arguments and returns a string representation of the date. Remember, the value of the year field represents year + 1900, and the value of the day field represents day + 1.
Note The ToString method is a little different from the methods you have seen so far. Every type, including structures and classes that you define, automatically has a ToString
method whether or not you want it. Its default behavior is to convert the data in a variable
to a string representation of that data. Sometimes, the default behavior is meaningful; other times, it is less so. For example, the default behavior of the ToString method generated for the Date class simply generates the string “StructsAndEnums.Date”. To quote Zaphod Beeblebrox in The Restaurant at the End of the Universe (Douglas Adams, Del Rey, 2005),
this is “shrewd, but dull.” You need to define a new version of this method that overrides the default behavior by using the override keyword. Overriding methods are discussed in
more detail in Chapter 12.
The ToString method should look like this:
public override string ToString()
{
return this.month + “ “ + (this.day + 1) + “ “ + (this.year + 1900);
}
Note The + signs inside the parentheses are the arithmetic addition operator. The others are the string concatenation operator. Without the parentheses, all occurrences of the + sign would be treated as the string concatenation operator because the expression being evaluated is a string. It can be a little confusing when the same symbol in a single expression denotes different operators!
5.Display the Program.cs file in the Code and Text Editor window.
6.Add a statement to the end of the Entrance method to declare a local variable named defaultDate, and initialize it to a Date value constructed by using the default Date constructor. Add another statement to Entrance to write the defaultDate variable to the console by calling Console.WriteLine.
Note The Console.WriteLine method automatically calls the ToString method of its argument to format the argument as a string.
182 |
Part II Understanding the C# Language |
|
The Entrance method should now look like this: |
|
static void Entrance() |
|
{ |
...
Date defaultDate = new Date(); Console.WriteLine(defaultDate);
}
7.On the Debug menu, click Start Without Debugging to build and run the program. Verify that the date January 1 1900 is written to the console. (The original output of the Entrance method will be displayed first.)
8.Press the Enter key to return to the Visual Studio 2008 programming environment.
9.In the Code and Text Editor window, return to the Entrance method, and add two more statements. The first statement should declare a local variable named halloween
and initialize it to October 31 2008. The second statement should write the value of halloween to the console.
The Entrance method should now look like this:
static void Entrance()
{
...
Date halloween = new Date(2008, Month.October, 31); Console.WriteLine(halloween);
}
Note When you type the new keyword, IntelliSense will automatically detect that there are two constructors available for the Date type.
10.On the Debug menu, click Start Without Debugging. Confirm that the date October 31 2008 is written to the console below the previous information.
11.Press Enter to close the program.
You have successfully used the enum and struct keywords to declare your own value types and then used these types in code.
If you want to continue to the next chapter:
Keep Visual Studio 2008 running, and turn to Chapter 10.
If you want to exit Visual Studio 2008 now:
On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Visual C# 2008 Express Edition) and save the project.
