Sams Microsoft Visual C Sharp 2005 Unleashed May 2006 ISBN 0672327767

Microsoft® Visual C# 2005 Unleashed

  By Kevin Hoffman ...............................................

  Publisher: Sams Pub Date: May 09, 2006 Print ISBN-10: 0-672-32776-7 Print ISBN-13: 978-0-672-32776-6 Pages: 720

   Setting the standard for a premium C# reference, Microsoft Visual C# 2005 Unleashed provides practical examples for virtually every aspect of the C# programming language.

The book is structured for progressive learning, so it can be read cover-to-cover or used as

a comprehensive reference guide. You will be exposed to everything from low-level

information on the Garbage Collector to advanced concepts, such as creating applications

that use Enterprise Services, creating Web Services, and even advanced Windows GUI. Chapters include: Expressions and Control Structures

  UI Controls Code Access Security Remoting Peer-to-Peer Applications

Microsoft® Visual C# 2005 Unleashed

  By Kevin Hoffman ...............................................

  Publisher: Sams

Pub Date: May 09, 2006

Print ISBN-10: 0-672-32776-7 Print ISBN-13: 978-0-672-32776-6 Pages: 720

  

  

  

  

  

  

  

  

  

  

  

  

  

  

Copyright Microsoft® Visual C# 2005 Unleashed

  Copyright © 2006 by Sams Publishing All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.

  Library of Congress Catalog Card Number: 2006901231 Printed in the United States of America First Printing: May 2006 09 08 07 06 4 3 2 1

Trademarks

  All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.

  Microsoft is a registered trademark of Microsoft Corporation.

Warning and Disclaimer

  Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an "as is" basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.

Bulk Sales

  Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales

   1-800-382-3419

  For sales outside the United States, please contact International Sales

   Publisher

  Paul Boger

  Acquisitions Editor

  Neil Rowe

  Development Editor

  Mark Renfrow

  Project Editor

  Tonya Simpson

  Copy Editor

  Margaret Berson

  Indexer

  Ken Johnson

  Proofreader

  Susan Eldridge

  Technical Editor

  J. Boyd Nolan

  Publishing Coordinator

  Cindy Teeters

  Interior Designer

  Gary Adair

  Cover Designer

  Alan Clements

Dedication

  Writing a technical book involves just as much creativity and inspiration as writing a fantasy or a science fiction novel; it's

just a different kind of inspiration. I would like to dedicate this

book to my inspiration: my wonderful wife, Connie.

  Regardless of where the inspiration comes from or what it produces, it's still a highly creative process that is subject to

writer's block and fits of irritability, crankiness, defeatism, and

being so absorbed in a particular topic that the world around

  me ceases to exist until I have conquered that topic.

It truly is a battle, "me vs. the technology." The problem is that

this battle requires a huge time investment. Somehow my wife

has managed to tolerate me being in the same room but simply

"not being there" while I type away in order to defeat the chapters that loom before me, daring me to take them on. My daughter, Jerrah, has her own keyboard so that she can pretend to "be busy" just like Daddy. There's something

bittersweet about the fact that she wants to be like me, but that

her impression of me is one of being busy and typing all the time.

  My wife's patience, love, caring, and support have been

invaluable. Without her support, I would've given up on writing

before I even got started. And certainly without her I never would have been able to write this book, the single largest and most time-consuming book I've ever written.

Regardless of which deity you believe in, or whether you believe

in one, you must concede the existence of angels. After all, I am married to one.

About the Author

  Kevin Hoffman has been programming for more than 20

  years. He got started when his grandfather repaired a discarded Commodore VIC-20 and he got right to work creating text- based role-playing games in BASIC. Since then, he has dedicated himself to learning all there is to know about programming, and using every programming language he could find, including Pascal, C, C++, Scheme, LISP, ADA, Perl, Python, Java, and many others. When he first saw a pre-beta release of C# 1.0, he was hooked. He knew at that moment that the .NET Framework would revolutionize how developers created software and how people designed software, and would enable new types of applications and new functionality that either used to be impossible or too cost-prohibitive to even attempt. As a result of his passion for the .NET Framework, he has dubbed himself the ".NET Addict."

We Want to Hear from You!

  As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. As a publisher for Sams Publishing, I welcome your comments. You can email or write me directly to let me know what you did or didn't like about this bookas well as what we can do to make our books better.

  Please note that I cannot help you with technical problems related to the topic of this book. We do have a User Services group, however, where I will forward specific technical questions related to the book.

  When you write, please be sure to include this book's title and author as well as your name, email address, and phone number. I will carefully review your comments and share them with the author and editors who worked on the book.

  Email: Mail: Paul Boger Publisher Sams Publishing 800 East 96th Street Indianapolis, IN 46240 USA

Reader Services

  Visit our website and register this book at

  

for convenient access to any

  updates, downloads, or errata that might be available for this book.

Introduction

  If you can force yourself to think back 10 years, think about the proliferation of technology in just that short period of time. Back in the "good ole" days, you could walk down a busy city street, and you probably wouldn't see anyone talking on a cell phone. You wouldn't see people busily tapping at their PDAs. You definitely didn't see anyone sitting in an Internet café using a Tablet PC to send hand-sketched application designs halfway around the world to a remote development team. Connectivity between applications and application components used to be something that required the use of extremely skilled, highly expensive development efforts. Sure, we had technologies like CORBA, but they had limited use.

  Today, connectivity itself has become so ubiquitous that it is

  demanded. When people turn on their PDA in an Internet-

  enabled coffee shop, they fully expect that they will be able to connect to the Internet from there. If they don'tthey will complain to the owner. When people flip open their brand new cellular phone, not only do they expect it to take photographs, but they expect to be able to upload their photos to a central location to be shared with friends, family, or the entire Internet. They expect that they can download ringtones, games, even music and videos. If they can'tthey want their money back.

  This same demand for connectivity, performance, and modern features can be found in desktop software and web software as well. People want their applications to follow the "It Just Works" principle. They should be able to beat on and otherwise abuse that application without fear of retribution in the form of lost or corrupted data. Users want their Windows applications to work properly whether they're connected to the Internet or not. They want their web applications to be more responsive than they to run fast, and they want them to remember what they like, what they don't like, and who they are. In short, the demands modern users are placing on the quality, features, and functionality of the applications they use have never been more strict. Programmers today must create some of the most powerful applications ever written, and they need to do it quickly, reliably, and cheaply. This book will take the developer through an in-depth exploration of the features, power, and capabilities of Visual C# .NET 2005, the latest and most powerful version of the C# language running on the .NET Framework 2.0. Among many other things, by reading this book you will

  Learn the fundamentals of the C# language and the core features of the .NET Framework 2.0, such as object- oriented programming, generics, and other basics such as delegates and event-driven programming.

  Learn how to work with data in many forms from many sources using ADO.NET 2.0, even how to write stored procedures for SQL Server 2005 in C#! Create Windows Forms applications with rich, highly responsive user interfaces.

  Deploy Windows Forms applications from the web or anywhere else with the click of a button.

  Create Windows Forms applications that update themselves automatically over the Internet by checking for new releases and patches. Learn how to create ASP.NET 2.0 applications and the new features that come with them. Utilize master pages, themes, and skins to create a consistent look and feel throughout your site and even provide the ability for users to select their own style theme. Use Web Parts and Web Part Pages to create dynamic, user- controllable content pages that contain information and functionality targeted to the specific user or the user's security level.

  Use the new Provider model in ASP.NET to create an application that can authenticate users, store user role membership, store user profile data, and user personalization information (such as Web Part preferences) in SQL Server 2005 in 15 minutes or less! Use client callbacks to create highly interactive, Ajax-style interfaces in ASP.NET 2.0.

  Learn how to create custom, reusable ASP.NET controls. Learn how to create and consume web services. Use remoting and/or COM+ to create distributed, connected applications.

  This book is crammed full of information on how to get you up to speed on the newest technology. It also provides you with practical application of the technology. As we all know, applying a technology just because it's new or interesting isn't necessarily the right thing to do. As you read this book, you will be introduced to the new technology from the ground up (learning to walk before you can run), as well as learning where that technology makes sense and when it should be applied.

  You might find some of the earlier chapters to be somewhat of a review if you have already been working with C# 2005. However, many of the later chapters in the book build on technologies and techniques introduced in earlier chapters, making this book ideally suited to being read from start to finish.

Part I: C# 2.0 Fundamentals In This Part CHAPTER 1 Introduction to C# 2.0

   Strings and Regular Expressions

   Arrays and Collections

  

  

Chapter 1. Introduction to C# 2.0 In This Chapter Introduction to C# 2.0 At A Glance What Is the .NET Framework? Working with Variables in C# 2.0 C# Basic Syntax What You Can Do with C# This chapter provides a brief introduction to the world of C#. As

  you will see, the syntax of the language itself is fairly easy to learn. The thing that takes the most time is learning how to program on the .NET Framework using the C# language. You will see the basic syntax of the C# language, including how to declare variables, create code blocks, and of course, create your first "Hello World" application.

  If you already have a working knowledge of the C# language from your experience with previous versions (either 1.0 or 1.1), you can skip this chapter and move on. Take care not to skip too many of the early chapters, as there have been enhancements to the language that are covered early on in the book, such as generics and anonymous methods. This chapter also skips over some details that you may not be aware of if you have very little exposure to programming. If you have never written software on the Windows platform (either Windows or Web-based), this is the wrong chapter, and perhaps the wrong book, for you.

What Is the .NET Framework?

  Before you learn the mechanics of the C# language itself, it is important that you learn the evolution of technologies that created the need for a language like C#. This section takes you on a quick tour of the history of C#, followed by an explanation of the Common Language Runtime, the Common Type System, and concluding with an overview of garbage-collected environments.

The Evolution of .NET

  Before the .NET Framework was released to the public, the majority of component-oriented development for the Windows platform took the form of COM objects. Component Object Model (COM), a binary standard for creating reusable components, allowed developers to write code that solved smaller problems in smaller problem domains. By breaking down the problem into components, the solution often became easier and the components used to solve the problem could be reused to solve other similar problems.

  COM offered many advantages and disadvantages. One of the most common problems with the COM standard was appropriately dubbed "DLL hell." DLL hell arose when COM interfaces were indexed in the registry and then newer versions of those components were released. Versioning often caused developers headaches due to the tightly coupled, binary nature of the standard. In addition, if a DLL's location changed on the file system without the information in the registry being modified, the COM interfaces contained within that DLL would become inaccessible. You could see examples of this when a new application was installed with a different version of some shared component, and it would not only break the new application, but all other applications using that component as well. Other problems with development at the time included things like difficulty managing memory, slow development times, GUI controls that were typically insufficient for many tasks, as well as a lack of interoperability between languages such as C++ and Visual Basic. As a solution to this and countless other problems with development at the time, Microsoft began working on what was then referred to as COM+ 2.0. The solution would provide a managed environment in which code would be executed that would provide for enhanced type safety, security, and an incredibly extensive library of useful classes and functions to make the lives of developers much easier. Eventually this solution became what is now the .NET Framework. Versions 1.0 and 1.1 have been released and now version 2.0 is available. In the next few sections, some of the key aspects of the .NET Framework will be discussed: the Common Language Runtime, the Common Type System, and the concept of garbage- collected managed code.

The Common Language Runtime

  Before talking about the basics of C#, you need to know a little bit about how C# (and all other .NET languages) works and where it sits in relation to the various components that make the entire .NET Framework run.

  One such component is the Common Language Runtime (almost exclusively referred to as the CLR). Unlike previous versions of C++ and similar languages, C# runs in a managed environment. Code that you write in C# runs inside the context of the Common Language Runtime. The runtime is responsible for managing things like memory and security and isolating your code from other code so that malicious or poorly written code can't interfere with the normal operation of your application.

Figure 1.1 illustrates the layers of .NET.

  

Figure 1.1. The layers of .NET.

  [View full size image]

  At the top layer you have the various .NET languages (this figure only includes a few of the languages that run under the CLR). Beneath those languages you have the Base Class Library. The BCL (Base Class Library) is the collection of classes and utilities written in the .NET Framework that provide you with the fundamentals you need in order to create your applications, such as code to allow you to deal with encryption, data access, file I/O, web applications, Windows applications, and much more. As you will see throughout the book, much of the task of learning C# isn't learning the language syntax; it's learning about the vast library of the BCL. In short, the CLR is the managed execution engine that drives the code written in the BCL and any applications you create.

  Don Box has written an excellent book on the Common Language Runtime that gives all the detail you can possibly imagine on what the CLR is and how it works. The book is called

  Essential .NET Volume I: The Common Language Runtime

  (ISBN 0201734117, Addison-Wesley Professional).

The Common Type System

  A common problem in many programming languages is the inability to exchange data between programs written in different languages. For example, you have to take special precautions when invoking C methods from Pascal, and Pascal methods from C because of parameter ordering. In addition, strings in C consist of an array of characters that are terminated with the

  \0

  ASCII NULL (typically represented as ). In Pascal, the first byte of a string actually contains the length of the string. These problems are just the tip of the iceberg. As you can tell, getting different languages to communicate with each other can be a nightmarish task (and, historically, it has been). A solution to this is to allow all of the languages to share a common representation for data types. The Common Type System provides for a common set of data types. For example, if you refer to a string in VB.NET, C#, J#, Delphi (.NET), managed C++, or any other .NET language, you are guaranteed to be referring to the same entity. This is because the type

  string is actually defined within the .NET Framework itself, not

  the language. Removing the data type definition from the languages creates an environment where VB.NET and C# code can coexist side by side without any communication issues.

Taking Out the Trash: Coding in a Garbage- Collected Environment

  As I mentioned earlier in this section, the CLR does quite a bit of memory management on your behalf. If you have used C# 1.0 or 1.1, you will be familiar with this concept. One of the declare new variables (we'll get to that in the next section) in C#, they are managed by the Garbage Collector. When a collection cycle takes place, the Garbage Collector examines your variables and if they are no longer in use, it will dispose of them (referred to as a collection). In

  your .NET 2.0 Code," you will learn more about the Garbage Collector (GC) and how awareness of its presence while coding and designing can improve the speed of your applications. For now, it is good enough to know that the GC is there managing your memory and cleaning up after you and your variables.

Working with Variables in C# 2.0

  My favorite analogy for explaining variables is the "bucket" analogy. Think of a variable as a bucket. Into that bucket, you can place data. Some buckets don't care what kind of data you place in them, and other buckets have specific requirements on the type of data you can place in them. You can move data from one bucket to another. Unfortunately, the bucket analogy gets a little confusing when you take into account that one bucket can contain a little note inside that reads "see Bucket B for actual data" (you'll read about reference types shortly in the section "Value Types vs. Reference Types").

  To declare a variable in C#, you can use the following syntax:

  type variable_name;

  You can initialize a variable on the same line:

  type variable_name = initialization expression type

  where is a .NET type. The next section lists some of the core .NET types.

Common .NET Types

Table 1.1 shows some of the basic .NET data types. As you will

  see in later chapters, this is just the beginning. When you start using classes, the variety of types available to you will be virtually unlimited.

  

Table 1.1. Core .NET Data Types

System.Boolean Data Type Description System.Byte Provides a way to store true/false data. System.Char Represents a single byte of data.

  A single character. Unlike other languages, this character is a 2-byte System.Decimal Unicode character. A decimal value with 28 to 29 significant digits in the range ±1.0 x System.Double 1028 to ±7.9 x 1028.

  A double-precision value that represents a 64-bit floating-point value with 15 to 16 significant digits in the range ±5.0 x 10324 to ±1.7 x System.Single 10308.

  A single-precision value that represents a 32-bit floating point number System.Int32 in the range ±1.5 x 1045 to ±3.4 x 1038. Represents a 32-bit signed integer in the range 2,147,483,648 to System.Int64 2,147,483,647. Represents a 64-bit signed integer in the range System.SByte 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. System.Int16 A signed 8-bit integer. System.UInt32 A signed 16-bit integer. System.UInt64 An unsigned 32-bit integer. System.UInt16 An unsigned 64-bit integer. System.String An unsigned 16-bit integer.

  An arbitrary-length character string that can contain Unicode strings. If you aren't familiar with .NET or C#, you may be wondering what the "System" is in the data types listed in .NET organizes all types into namespaces. A namespace is a logical container that provides name distinction for data types. These core data types all exist in the "System" namespace. You'll see more namespaces throughout the book as you learn about more specific aspects of .NET.

Type Shortcuts

  C# provides you with some shortcuts to make declaring some of the core data types easier. These shortcuts are simple one- word lowercase aliases that, when compiled, will still represent a core .NET type. lists some data type shortcuts and their corresponding .NET types. Shortcut .NET Type Table 1.2. C# Aliases for .NET Data Types byte System.Byte bool System.Boolean decimal System.Decimal char System.Char float System.Single double System.Double long System.Int64 int System.Int32

  short System.Int16 sbyte System.SByte ulong System.UInt64 uint System.UInt32 ushort System.UInt16

Value Types vs. Reference Types

  Up to this point, this chapter has just been illustrating data types in one category. Earlier in the chapter, I mentioned a "bucket" analogy where data in one bucket could actually refer to data contained in some other bucket. This is actually the core point to illustrate the difference between value types and reference types.

  A value type is a type whose data is contained with the variable on the stack. Value types are generally fast and lightweight because they reside on the stack (you will read about the exceptions in

  stack, but instead resides on the heap. When the data contained in a reference type is accessed, the contents of the variable are examined on the stack. That data then references (or points to, for those of you with traditional C and C++ experience) the actual data contained in the heap. Reference types are generally larger and slower than value types. Learning when to use a reference type and when to use a value type is something that comes with practice and experience.

  Your code often needs to pass very large objects as parameters to methods. If these large parameters were passed on the stack as value types, the performance of the application would degrade horribly. Using reference types allows your code to pass a "reference" to the large object rather than the large object itself. Value types allow your code to pass small data in an optimized way directly on the stack.

C# Basic Syntax

  You will learn a lot of C# syntax tricks as you progress through the book. This section introduces you to the most basic concepts required to create the simplest C# applicationthe canonical "Hello World" sample.

Code Blocks

  As you know, all programming languages work on the same basic principle: individual instructions are executed in sequence to produce some result. Instructions can be anything from defining a class to printing information to the Console output.

  In C#, multiple lines of code are grouped together into a logical execution block by wrapping the lines of code with the "curly brackets" or braces: the { and } symbols.

The Canonical "Hello World" Sample

  It seems as though virtually every book on a programming language starts off with a program that prints the phrase "Hello World" to the console. Rather than risk horrible karma and unknown repercussions from breaking with tradition, I am going to inflict my own version of "Hello World" upon you. To start, open up whatever 2005 IDE you have (C# Express 2005, or any of the Visual Studio 2005 editions) and create a new Console Application (make sure you select C# if you have multiple languages installed) called HelloWorld . Make sure that your code looks the same as the code shown in

Listing 1.1. The Hello World Application

  using System; using System.Collections.Generic; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("--- Visual C# 2005 Unleashed ---"); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Hello "); Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("World"); } } }

  Console

  The class (you'll learn about classes in

  "Objects and Components") provides a set of methods for dealing with the console. In previous versions of .NET, you could not do anything with colors in the Console class. With .NET 2.0, you now have the ability to change the foreground and background colors of the console. Press Ctrl+F5 or choose Debug and then Start without Debugging.shows the output of the application, including the newly added color features of the 2.0 Console.

  

Figure 1.2. The Hello World Application, in full

color.

What You Can Do with C#

  As you will see throughout this book, you can produce all kinds of applications using C#. The C# language, and the .NET Framework underneath it, allow you to create

  Windows applications Create compelling, fast, powerful desktop applications that can do amazing things, including update themselves over the web automatically. Web applications Create amazingly powerful, rich, full- featured web applications in the shortest amount of time ever, taking advantage of new features like Web Parts, Master Pages, Themes, and much more.

  Web services Get on board with Service-Oriented Architecture by exposing business logic and data over industry-standard protocols.

  Data-driven applications Consume data from a wide variety of sources, aggregate data from multiple sources, and expose that data to end users through Web or Windows interfaces.

  Mobile applications Create web applications that automatically recognize mobile platforms and adapt to the small form factor and limited capacity of PocketPCs and cellular phones.

  Mobile clients Create applications that target the PocketPC platform and can not only communicate with other desktop applications, but can also communicate with the Internet, consume web services, and interact with SQL databases.

  SQL Server 2005 Create stored procedures, functions, and user-defined data types that reside directly inside the SQL Server 2005 database engine for unprecedented productivity, performance, and reliability.

  Integration applications Take advantage of COM Interoperability as well as .NET-based APIs to integrate your own code with other applications such as Office 2003, SharePoint Portal Server 2003, and much, much more.

Summary

  This chapter provided you with a brief introduction to the world of the .NET Framework and C# 2.0. You read about how the .NET Framework evolved as well as what it is now. In addition, you were introduced to the Common Language Runtime, the Garbage Collector, the Common Type System, and finally a very small introductory application.

  This chapter has provided you with the basic foundation that you will need in order to continue learning about C# 2005 by progressing through this book.

Chapter 2. Expressions and Control Structures In This Chapter Branching and Conditional Logic Looping and Repetition At the core of every language is the ability to perform two different types of tasks: repetition and conditional logic. Repetition involves using language constructs called loops to

  perform the same task multiple times. Conditional logic involves writing code that will only execute certain code based on specified conditions. This chapter introduces you to the basics of how to accomplish both of these types of tasks using the C# language. If you have any familiarity with programming languages at all, the concepts discussed in this chapter should seem familiar and will be easy for you to pick up the C# syntax. If you have already been using C# for some time, you should be able to skim this chapter briefly as a refresher before continuing on through the rest of the book.

Branching and Conditional Logic

  Every modern programming language supports the notion of branching and conditional logic. This gives your applications the ability to do different things based on current input, events, error conditions, or any condition that can be expressed logically. This section first shows you how C# allows you to form logical expressions and how to create code blocks that are executed conditionally based on those expressions. Then you'll see a couple of shortcuts and extras that C# contains to make using certain types of common logic statements even easier.

Introduction to Boolean Expressions

  A Boolean expression is a code statement that will eventually evaluate to either true or false At the most basic level, all Boolean expressions, no matter how long or complex, are still going to evaluate to either true or false .

  The simplest of all Boolean expressions is equality. This expression is used to test whether or not one value is equivalent to another value. This can be something simple, such as

  2 == 4

  This expression will evaluate to false because 2 is not equal to

  4. It can also be something complex, such as

  MyObject.MyProperty == YourObject.YourProperty

  This expression could evaluate to anything, and would be determined at runtime. If you are familiar with C, C++, or even C#, you know that the == (double-equals) is a logical Boolean operator, and the = (single equals) is an assignment operator used to set values. A common source of runtime and compile- time errors revolves around using these operators in the wrong place.

Table 2.1 shows the logical operators available in the C# language and provides a brief description of their purpose.

  Table 2.1. C# Logical Operators Operator Description && A Boolean AND operator. Expressions involving this operator will evaluate to true if both the expression on the left side of the operator and the expression on the || right side of the operator evaluate to true .

  A Boolean OR operator. Expressions using this operator will evaluate to true if either the left expression or the right expression are true, or if both expressions are ! true.

  The NOT operator. It negates the Boolean value of whatever expression it is applied to. So, if ! is applied to a true expression, the expression will become false. Likewise, if ! is applied to a false expression, the ^ expression will become true.

  The exclusive OR operator (often referred to as XOR in many languages). This functions the same way as the standard OR (||) operator, with the exception that if both sides of the expression are true, this operator will ~ return false This operator performs a bitwise complement on its operand. This means that it will reverse every single bit in whatever operand is supplied. On a single-bit value, it is the same as the operator. However, on numbers ! such as integers, decimals, and so on, it has a more profound effect.

  AND against two Boolean expressions, it performs it against two numeric values. The numbers are lined up, and the AND is performed against each bit. The result of this operation is a new number. Bitwise logic has very specific uses, often when storing long lists of Boolean | values as numbers.

  The bitwise OR operator. As with the bitwise AND operator, it performs an OR operation against each bit of the two operands supplied for the expression, the result being returned as a numeric value.

Using Basic Conditional Statements

  In the preceding section you were introduced to the tools you need in order to form Boolean expressions. These will allow you to assert whether or not an expression is true. When you have such an assertion, you need to be able to provide some conditional statements in your code to allow you to do something meaningful based on the results of a Boolean expression. This section will show you the basic conditional statements that are at the heart of almost all logic code in C#.

  If/Else Statements

Using

  The format of an if statement is as follows:

  if ( expression ) code_block else if ( expression_1 ) code_block else if ( expression_2 ) code_block else code_block else if else

  Both the and sections are optional, and are only required if you want your code to perform alternate tasks when the original Boolean expression evaluates to false . Because each of the code blocks in the preceding example can also contain their own if statements, you can nest your conditional code nearly as deep as you like. However, good style and etiquette recommend that you avoid deep nesting because it makes your code difficult to read and analyze. The following sample if statements illustrate using if simply, nested, and with else statements:

  if ( strInput == "Hello" ) Console.WriteLine("You said Hello"); if ( strInput2 == "Goodbye" ) Console.WriteLine("You said Goodbye"); else if ( strInput2 == "Later" )

Console.WriteLine("You didn't say goodbye, you said Later.")

else { if (strInput3 == "Hola") if (strInput4 == "Senor") Console.WriteLine("Hello!"); }

  Switch Using the Statement

  If you want to test a single variable against a list of possible

  values using just the standard if/else keywords, you will end up with code that looks like the following:

  if (val == 1) ... else if (val == 2) ... else if (val == 3) ... else if (val == 4) ... else ...

  Although this may get the job done, it's not the most elegant or the most easily readable block of code. For this situation, C# has the switch statement, which allows you to combine several logic tests into a single expression, as shown in the following example:

  switch (val) { case 1: ... break; case 2: ... break; case 3: ... break; default: ... }

  Goto

Using the Statement

  The goto statement has two main uses; one is to transfer control from one case within a switch statement, and the other is to break out of loops. You will see more on loops later in this chapter.

  switch

  The following is a sample of a statement that utilizes the

  goto keyword for transferring control: switch (val) { case 1: ... break; case 2: ... goto case 1; break; case 3: ... goto case 1; break; default: ...

  } Using Advanced Conditional Statements

  One of the most common uses for an if/else statement is to conditionally print something or render something through Windows Forms or Web Forms. For example, suppose that you want to print the word "Good" if the profit margin is greater than 20, or "Bad" if the profit margin is less than or equal to 20.

  if/else

  You might use an that looks like the following:

  if (profitMargin > 20) Console.WriteLine("The profit margin is Good!"); else Console.WriteLine("The profit margin is Bad!");

  This is perfectly fine, but C# includes a ternary operator called

  conditional if/else

  the operator that allows you to embed an combination in a single line. You can rewrite the preceding code segment in a single line as follows:

  

Console.WriteLine("The profit margin is " + (profitMargin > 20) ? "Good" : "Bad");

  The ternary conditional operator has the following format:

  expression ? return_when_true : return_when_false

Looping and Repetition

  In addition to conditional logic and branching, looping is one of the most common tasks performed by all applications, regardless of the application type. It is impossible to find a production-quality application written in .NET today that does not include some use of loops. A loop is a repetition of a code block a certain number of times. The number of times is determined by the type of loop being used. This section will show you the basics of creating and using loops in C#.

  for

Using the

Loop

  Each for loop has three sections:

  

1. The initializer. This is a block of code that is executed once

  at the beginning of the for loop. Variables declared in this section are available for the scope and duration of the loop but will not remain outside the loop. This section is typically used to initialize the counter variable or variables.

  2. The exit condition. This section defines a Boolean

  expression that is used to determine how long the for loop keeps running. As long as the Boolean expression continues to evaluate to

  true

  , the loop keeps running. This expression is tested at the beginning of each loop execution.

  

3. The iterative code. This section is arbitrary, and can contain

  any code that you want to execute at the end of each iteration through the loop. This is typically where the loop counter variables are incremented. for ( initializer; exit condition; iterative code ) { ... } for

  So, to create a loop that will execute a block of code five times, you would write the following:

  for ( int x = 0; x < 5; x ++)

Console.WriteLine("The value is " + x.ToString() );

  This will print the following output:

  1

  2

  3

  4 while

Using the Loop

  for

  Whereas the loop is typically used to provide an indexed (a counter variable) loop, the while loop will continually perform the same action over and over again so long as a given Boolean

  true

  expression evaluates to . The format of this loop is as follows:

  while ( expression ) { ... }

  If you wanted to perform an action until some flag indicated that you could not perform it any more, your while xloop might look something like the following:

  bool canContinue = true; while (canContinue) { ... perform action ... canContinue = ... }

  The expression is evaluated at the beginning of each loop execution, and if the evaluation is false, the code within the loop will not be executed. A common mistake when using while loops is neglecting to set the exit condition for the loop. In the

  canContinue

  preceding example, this is the variable. If you don't set the exit condition for the while loop, it will run forever (or until you shut the application down forcefully).

  do

Using the Loop

  The do loop is similar to the while loop, except that the Boolean expression is evaluated at the end of the loop execution instead of at the beginning of the loop. The format of the do loop is as follows:

  do { ... } while ( expression );

  The use of the do loop always guarantees that the code inside the loop will execute at least once. All the other loop types have the ability to execute 0 times if the entry condition fails. Here is a sample do loop:

  int x = 1; do { Console.WriteLine(x); x++; } while ( x < 10 );

Summary

  Whether you're working with a database, 3D graphics, a console application, a Windows Forms application, or a Web Forms application, there are a few types of tasks that need to be performed: looping and branching.

  Looping and branching are at the core of any language's definition. This chapter has shown you how you can use C# to create complex branching using Boolean expressions as well as multiple ways to perform iterative programming tasks by using loops. This chapter has presented some information that, on its own, may not appear to be entirely useful. However, when you look at virtually every programming problem you will ever face in the future, you will be hard-pressed to find a scenario that does not involve looping and branching in some respect.

Chapter 3. Strings and Regular Expressions In This Chapter Working with Strings Working with Regular Expressions Regardless of what type of data you're working with or what

  kind of application you're creating, you will undoubtedly need to work with strings. No matter how the data is stored, the end user always deals in human-readable text. As such, knowing how to work with strings is part of the essential knowledge that any .NET developer needs to make rich and compelling applications.

  In addition to showing you how to work with strings in the .NET Framework, this chapter will also introduce you to regular expressions. Regular expressions are format codes that not only allow you to verify that a particular string matches a given format, but you can also use regular expressions to extract meaningful information from what otherwise might be considered free-form text, such as extracting the first name from user input, or the area code from a phone number input, or the server name from a URL.

Working with Strings

  Being able to work with strings is an essential skill in creating high-quality applications. Even if you are working with numeric or image data, end users need textual feedback. This section of the chapter will introduce you to .NET strings, how to format them, manipulate them and compare them, as well as other useful operations.

Introduction to the .NET String

  Before the .NET Framework and the Common Language Runtime (CLR), developers used to have to spend considerable amount of effort working with strings. A reusable library of string routines was a part of virtually every C and C++ programmer's toolbox. It was also difficult to write code that exchanged string data between different programming languages. For example, Pascal stores strings as an in-memory character array, where the first element of the array indicated the length of the string. C stores strings as an in-memory array of characters with a variable length. The end of the string was indicated by the ASCII null character (represented in C as \0 ).

  In the .NET Framework, strings are stored as immutable values. This means that when you create a string in C# (or any other .NET language), that string is stored in memory in a fixed size to make certain aspects of the CLR run faster (you will learn more about this in