csharp mcsd 70-315, 70-316, and 70-320 visual c# net certification

1067
CONTENTS Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii Part 1 C#: The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1 Introduction to C# .NET and the .NET Platform . . . . . . . . . . . . . . . . 3 Overview of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 So, What Is .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Application Development in .NET . . . . . . . . . . . . . . . . . . . . . . . . . 5 The .NET Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 The CLS—Common Language Specifications . . . . . . . . . . . . . . . . . 7 The CLR—Common Language Runtime . . . . . . . . . . . . . . . . . . . . . 8 MSIL—Microsoft Intermediate Language . . . . . . . . . . . . . . . . . . . . 9 The BCL—Base Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 The .NET Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Microsoft C# .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Basic C# Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Object-Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . 20 A Brief History of Object-Oriented Programming . . . . . . . . . . . . . 20 Just What Is an Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Structs Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 The convert Routine Using an Object-Oriented Approach . . . . . . 24 Properties and Methods of an Object . . . . . . . . . . . . . . . . . . . . . . . 25 Classes—Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Instance Data vs. Class Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Abstraction and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 ix

Upload: avinash

Post on 13-Dec-2014

172 views

Category:

Documents


20 download

TRANSCRIPT

CONTENTSAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxvii

Part 1

C#: The Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overview of .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . So, What Is .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Application Development in .NET . . . . . . . . . . . . . . . . . . . . . . . . . The .NET Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The .NET Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The CLSCommon Language Specifications . . . . . . . . . . . . . . . . . The CLRCommon Language Runtime . . . . . . . . . . . . . . . . . . . . . MSILMicrosoft Intermediate Language . . . . . . . . . . . . . . . . . . . . The BCLBase Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The .NET Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Microsoft C# .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Basic C# Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Object-Oriented Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . A Brief History of Object-Oriented Programming . . . . . . . . . . . . . Just What Is an Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Structs Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The convert Routine Using an Object-Oriented Approach . . . . . . Properties and Methods of an Object . . . . . . . . . . . . . . . . . . . . . . . ClassesPutting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . Instance Data vs. Class Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstraction and Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

134 4 5 6 6 7 8 9 10 11 11 12 16 17 17 18 19 20 20 22 23 24 25 28 28 29 30 34 34 36

Chapter 1 Introduction to C# .NET and the .NET Platform . . . . . . . . . . . . . . . .

ix

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xChapter 2 Variables and Program Structure ............................ 3940 40 42 42 43 44 45 46 46 46 47 47 48 49 50 51 53 53 53 54 55 55 56 57 60 62 67 67 68 69 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Value-Type Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reference-Type Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Variable Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Value-Type Variables on the Stack . . . . . . . . . . . . . . . . . . . . . . . . . . The Parts of Reference-Type Variables . . . . . . . . . . . . . . . . . . . . . . . Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implicit Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conversion Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Boxing and Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Destroying Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . Cleaning Up the Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Structure of a C# Class File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Namespaces and the Using Directive . . . . . . . . . . . . . . . . . . . . . . . Coding Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lexical Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Constants and Read-Only Variables . . . . . . . . . . . . . . . . . . . . . . . . Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Chapter 3 Advanced Object-Oriented Concepts . . . . . . . . . . . . . . . . . . . . . . . .Introduction to Advanced OO Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Methods to Hide Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Properties to Hide Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inheritance Explained . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Method Overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Sealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Use Interfaces? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extending Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7172 72 74 75 75 87 87 91 92 93 94 95

Contents

xiOverloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 97 98 100

Chapter 4 Strings, Exceptions, and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . System.String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String Building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The System.Array Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Overview of Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Throwing and Catching Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Coding to Handle Errors and Exceptions . . . . . . . . . . . . . . . . . . . . Creating Your Own Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

101101 102 104 106 106 107 109 110 111 112 118 118 120 123 125 125 128

Chapter 5 Visual Studio .NET Development Environment . . . . . . . . . . . . . . . . .What is Visual Studio .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Start Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Get Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Online Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Types of Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Grouping Projects into Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . Customizing Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Customizing with the Start Page . . . . . . . . . . . . . . . . . . . . . . . . . . . Customizing with the Options Dialog Box . . . . . . . . . . . . . . . . . . . The Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Explorers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other Highlights of Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

131131 132 133 135 136 137 138 138 139 140 142 142 143 144 144 148 153 153 153 154

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xiiPart II The C# Exams: Common Elements . . . . . . . . . . . . . . . . . . .Chapter 6 Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The .NET Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Versioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is in the Assembly? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with a .NET Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Modifying a .NET Assembly . . . . . . . . . . . . . . . . . . . Strongly Named Assemblies and GAC . . . . . . . . . . . . . . . . . . . . . . Creating and Implementing Satellite Assemblies . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

155157157 158 158 159 159 160 161 163 164 165 165 166

Chapter 7 Resources and Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .String Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Localized Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing Right-to-Left Mirroring and Encoding . . . . . . . . . . . . . . . Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

167168 171 174 175 176 176 179

Chapter 8 XML and C# Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HTML vs. XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why Use XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Declaration and Processing Instructions . . . . . . . . . . . . . . . . Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Well-Formed XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Documentation in C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Documentation Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Comments and Visual Studio .NET . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

181181 182 183 186 186 187 187 187 188 188 189 190 190 194 194 196

Contents

xiiiChapter 9 Debugging and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Debugging Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Buggy Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Two DebuggersCorDbg and DbgCLR . . . . . . . . . . . . . . . . . . . . . Using the Visual Studio .NET Debugging Capabilities . . . . . . . . . . .NET Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging an ASP.NET Application . . . . . . . . . . . . . . . . . . . . . . . . Testing Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Purpose of Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Developing a Test Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

197198 198 200 204 209 215 216 216 217 220 220 221

Chapter 10 Basic Data-Access Techniques

..............................

223223 224 228 233 233 233 243 245 246 246 246 248 249 251 253 254 255 256 258 259 260 262 263 264 265 268

ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Application Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ADO.NET Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML and ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data-Access Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ACID Rules (Transactions) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Connecting to a Data Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Database Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Connection Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Connection Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Command Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The DataSet Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ADO.NET and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Loading XML into a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Writing XML from a DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the XmlDataDocument Object . . . . . . . . . . . . . . . . . . . . . . . Using the DataAdapter to Access Existing Data . . . . . . . . . . . . . . . . . . . . Creating the DataAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Updating the Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Resolving Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xivPart III Exam 70-315: Developing and Implementing Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Chapter 11 Welcome to the Internet and ASP.NET . . . . . . . . . . . . . . . . . . . . . . .The Internet: Its Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The TCP/IP Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HTTP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Internet Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Static Web Pagesthe Beginning . . . . . . . . . . . . . . . . . . . . . . . . . . Dynamic Web Pagesthe Client Story . . . . . . . . . . . . . . . . . . . . . . Dynamic Web Pagesthe Server Story . . . . . . . . . . . . . . . . . . . . . . The Internet Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Client-Side Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Server-Side Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello Web World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Object Model of ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Request Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Response Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Server Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . State or No State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Session Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

269271271 272 274 274 275 276 277 279 280 282 282 283 285 285 288 289 291 293 293 295 297 298 298 301

Chapter 12 Web Forms: Client-Side Programs . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction to Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building Your First Web Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Controls to a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Validation Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom and User Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Address Book Web Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

303303 304 310 311 316 326 331 331 342 343 346

Contents

xvChapter 13 Server-Side Programs and Postback Operations . . . . . . . . . . . . . . . .The ASP.NET Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ASP.NET Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Page Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . State Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Write a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Consume a Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . COM Interoperability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Platform Invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ASP.NET and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Postback Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

347348 348 348 355 365 366 370 374 378 380 382 387 387 391

Chapter 14 Server Controls in ASP.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with ASP Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Base Properties of Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . HTML Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Web Server Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . User Controls and Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building and Using a User Control . . . . . . . . . . . . . . . . . . . . . . . . . Building and Using a Custom Control . . . . . . . . . . . . . . . . . . . . . . Dynamic Control Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

393393 395 396 399 404 404 408 413 415 415 418

Chapter 15 Web Forms and User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Importance of the User Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Resource Manager Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Validation Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom Error Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page_Error() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Event Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Page-Level Tracing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

419419 421 426 429 431 435 436 438 438 440 446 446 449

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xviChapter 16 Consuming and Manipulating Data from Data Sources . . . . . . . . . . .Visual Studio .NET Support for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using a DataGrid Server Control . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Data Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Connecting to a Data Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating and Configuring a Command Object . . . . . . . . . . . . . . . Populate the DataSet with the Data from the Command . . . . . . . The Completed Web Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building a Data Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating the SQL Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building the Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Retrieving Real-Time Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Looking Up the Address Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

451452 453 458 463 466 467 468 470 470 471 472 472 472 480 483 486 486 489

Chapter 17 Making the Web Application Available to Our Users . . . . . . . . . . . .Deploying a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Caching and Improving Performance . . . . . . . . . . . . . . . . . . . . . . . Configuring a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . Securing a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deploying a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing a Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing IIS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing FrontPage Server Extensions . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

491492 492 496 502 509 515 515 517 517 518 521

Part IV

Exam 70-316: Developing and Implementing Windows-Based Applications with Microsoft Visual C# .NET and Microsoft Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . System.Windows.Forms Namespace and More . . . . . . . . . . . . . . .

523525525 527

Chapter 18 Introduction to Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Contents

xviiWhat Is a Form? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Windows Forms Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Closer Look at Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Visual Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Localizing Your Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Controls to a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Types of Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting Properties of Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is an Event? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What Happens When an Event Occurs? . . . . . . . . . . . . . . . . . . . . . Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 536 541 545 547 549 550 553 556 557 557 557 558 559 562 562 567

Chapter 19 User-Interface Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Working with Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Specifying a Controls Location on a Form . . . . . . . . . . . . . . . . . . . Aligning Controls on a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Managing Control Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Windows Forms Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Buttons, Text Boxes, and Labels, Oh My! . . . . . . . . . . . . . . . . . . . . List Boxes, Combo Boxes, Checked List Boxes, and Such . . . . . . . Tab Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Status Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with Controls at Run Time . . . . . . . . . . . . . . . . . . . . . . . . Working with Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Main Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Looking at the Code of a Main Menu . . . . . . . . . . . . . . . . . . . . . . . Handling Menu Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Validating User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Validating Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ErrorProvider Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

569570 570 572 576 577 578 586 588 589 590 591 595 597 597 599 600 601 602 603 604 605 606 609

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xviiiChapter 20 Data in Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Review of ADO.NET Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Data Using Visual Studio .NET . . . . . . . . . . . . . . . . . . . . . . . . . The College Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ADO.NET Objects in Visual Studio .NET . . . . . . . . . . . . . . . . . . . . Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Whats Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Theory of Data Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binding Data at Design Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Binding Data at Run Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . BindingContext and CurrencyManager . . . . . . . . . . . . . . . . . . . . . . Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing Data Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Connection Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Command Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DataAdapter and DataSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DataReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Format and Parse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Format Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Parse Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

611611 613 613 614 625 626 627 628 628 631 634 636 638 640 641 642 644 645 645 646 646 646 651

Chapter 21 Web Services and COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How Do Web Services Work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building a Simple Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . Consuming a Simple Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . How Web Services Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . COM and COM+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NET and COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Working with COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Behind the Scenes with COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Platform Invoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

653653 654 654 659 663 666 667 667 669 672 673 674 677

Chapter 22 Build Your Own Windows Control . . . . . . . . . . . . . . . . . . . . . . . . . .Methods of Building Windows Controls . . . . . . . . . . . . . . . . . . . . . . . . . . Extending an Existing Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

679680 680

Contents

xixCreating Composite Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Visual Studio .NET Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using Visual Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Design-Time Support to Your Control . . . . . . . . . . . . . . . . . . . . . Adding a Toolbox Bitmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Attributes to Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . Providing Other Design-Time Support . . . . . . . . . . . . . . . . . . . . . . Hosting Your Control in Internet Explorer . . . . . . . . . . . . . . . . . . . . . . . . Licensing a Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687 696 697 697 702 702 705 706 706 707 709 709 717

Chapter 23 Deploying a Windows-Based Application . . . . . . . . . . . . . . . . . . . . . .Review of Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strong Named Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing an Assembly into GAC . . . . . . . . . . . . . . . . . . . . . . . . . . . Precompiling Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deploying a Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Setup Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Web Setup Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a CAB Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a Merge Module Project . . . . . . . . . . . . . . . . . . . . . . . . . . Conforming to Windows Standards . . . . . . . . . . . . . . . . . . . . . . . . Security Policies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

719719 720 721 722 723 723 734 735 737 738 738 740 740 743

Chapter 24 Configuring a Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . .Configuring a Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Machine Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Application Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . Security Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NET Framework Configuration Tool . . . . . . . . . . . . . . . . . . . . . . . XML Configuration Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Optimizing a Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debug vs. Release Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Precompiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other Performance Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . Securing a Windows Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Code-Access Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Role-Based Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

745745 746 747 748 749 757 757 757 760 761 763 763 768

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xxProviding User Assistance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Providing User Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding Accessibility Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Localizing a Windows Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using System.Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 769 776 778 779 781 782 786

Part V Exam 70-320: Developing XML Web Services . . . . . . . . . . .Chapter 25 Introduction to XML Web Services and Server Components . . . . . .The Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML (Extensible Markup Language) . . . . . . . . . . . . . . . . . . . . . . . . SOAP (Simple Object Access Protocol) . . . . . . . . . . . . . . . . . . . . . . XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . UDDI (Universal Description, Discovery, and Integration) . . . . . XSD, XSLT, and XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Logging Test Results and Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Control Debugging in web.config . . . . . . . . . . . . . . . . . . . . . . . . . . Logging Test Results in an XML Web Service . . . . . . . . . . . . . . . . . Implementing Tracing in an XML Web Service . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

787789790 790 793 799 808 811 817 817 819 821 821 822 827

Chapter 26 Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introducing Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building a Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating an Installer for the Service . . . . . . . . . . . . . . . . . . . . . . . . . Creating the Setup Project for the Windows Service . . . . . . . . . . . Installing the Windows Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . Verifying the Service Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Windows Service Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Quick Facts on Windows Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Monitoring the Windows Service ......................... Server Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Multi-Tiered Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . COM and COM+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Serviced Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the Component Services Tool . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

829829 832 836 838 842 846 848 849 850 855 855 857 858 866 871 872 879

Contents

xxiChapter 27 .NET Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .What Is .NET Remoting? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Terms and Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NET Remoting Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Steps for Invoking a Server Objects Methods . . . . . . . . . . . . . . . . . Using .NET Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building the Remote Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building the Remote Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running the Remote Application . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring .NET Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating the Remote Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating the Remote Object Configuration File . . . . . . . . . . . . . . . Creating the Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating the Client Configuration File . . . . . . . . . . . . . . . . . . . . . . Setting Up IIS to Host the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . Client-Activated Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Managing the Lifetime of a Server Object . . . . . . . . . . . . . . . . . . . . . . . . . Final Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Performance Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .NET Remoting vs. Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . Points to Remember . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

881881 882 885 889 890 890 891 893 894 896 896 897 898 900 900 903 904 905 906 906 907 907 908 914

Chapter 28 XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introducing XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Distributed Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Services in a Nutshell . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Services Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Service Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Service Consumer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Service Broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Services Programming Model . . . . . . . . . . . . . . . . . . . . . Building an XML Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing the Web Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting the Web Method Attributes . . . . . . . . . . . . . . . . . . . . . . . . . The Project Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Web Service Consumers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Discovering XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . Proxies for XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building an XML Web Service Consumer . . . . . . . . . . . . . . . . . . . .

917917 917 920 922 923 924 924 925 925 926 933 936 937 939 939 943 945 945

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

xxiiAsynchronous Web Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . SOAP Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951 952 953 953 956

Chapter 29 Data Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .ADO.NET and XML Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ADO Objects Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DataSet Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DataSet Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . . . . . . XmlReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Transformations and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Validating XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Microsoft SQL Server and XML Support . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

957957 957 960 960 966 976 976 980 983 986 990 990 991 994

Chapter 30 Deployment and Security

..................................

997997 997 1003 1003 1003 1010 1012 1014 1015 1016 1017 1022

Deploying an XML Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . UDDI Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Securing an XML Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Secure Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Access Unmanaged Code Using InterOp . . . . . . . . . . . . . . . . . . . . . . . . . . Using a COM Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Test Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Part VI Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1023

Appendix A About the CD-ROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025 LearnKey Online Training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025

Contents

xxiiiInstalling and Running MasterExam and MasterSim . . . . . . . . . . . . . . . . MasterExam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . MasterSim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Electronic Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removing Installation(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Technical Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . LearnKey Technical Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026 1026 1026 1026 1027 1027 1027 1027

Appendix B The New Microsoft Developer Tracks . . . . . . . . . . . . . . . . . . . . . . . . 1029What Is an MCSD for Microsoft .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . What Is an MCAD for Microsoft .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Prepare for Microsoft Exams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What the Questions Might Look Like . . . . . . . . . . . . . . . . . . . . . . . The Logistics of the Exam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exam Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exam 70-315 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exam 70-316 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exam 70-320 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029 1031 1032 1032 1033 1035 1035 1036 1038 1039

Appendix C Command-Line Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1041Command Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuration and Deployment Tools . . . . . . . . . . . . . . . . . . . . . . Security Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . General Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . C# Compiler (csc.exe) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . csc Command-Line Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1041 1042 1042 1043 1044 1045 1045 1047

Appendix D The .NET Class Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049Accessing Classes from the .NET Class Library . . . . . . . . . . . . . . . . . . . . . The System Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A Final Note on the System Namespace . . . . . . . . . . . . . . . . . . . . . Useful System Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . System.Data Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . System.Web.UI Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The System.Windows.Forms Namespace . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049 1052 1052 1056 1057 1057 1060 1060 1061

Appendix E Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1063A B C D E F G H I J L M N O P R S T U V W X .......................................................... .......................................................... .......................................................... ......................................................... .......................................................... .......................................................... .......................................................... ......................................................... .......................................................... .......................................................... .......................................................... ......................................................... ......................................................... ......................................................... .......................................................... .......................................................... .......................................................... .......................................................... ......................................................... .......................................................... ......................................................... .......................................................... 1063 1064 1065 1066 1067 1068 1068 1068 1068 1069 1069 1069 1069 1070 1070 1070 1071 1072 1072 1073 1073 1073

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1075

C#: The Language

Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5

Introduction to C# .NET and the .NET Platform Variables and Program Structure Advanced Object-Oriented Concepts Strings, Exceptions, and Events Visual Studio .NET Development Environment

1

CHAPTER

Introduction to C# .NET and the .NET PlatformIn this chapter, you will Learn about the .NET Framework Become familiar with the .NET languages Discover the environment surrounding C# .NET Compile your first C# program Learn the basics of the C# language Be introduced to object-oriented programming Learn to use abstraction and encapsulation Learn to differentiate between instance data and class data

1

If you have been around the programming world for a while, you may have some experience with languages such as Visual Basic, C++, or Java. These languages commonly provide desktop solutions, and over the years have had to expand their reach to include enterprise development. In order to accommodate the concepts of distributed programming, reusable software, and platform independence, languages were extended and programming structures were put into place. These additions include such things as Component Object Model (COM), Distributed Component Object Model (DCOM), Transaction servers and Application servers. Microsoft was quick to notice that a shift in development practices began with the introduction of Internet programming. The Internet necessitated some kind of common interface to be placed in front of the user. The software languages were struggling to provide scripted or interpreted environments that could run in a browser on every kind of platform. If you have ever had to work with plug-ins, browser incompatibility, slow scripted languages, or out-of-date runtime environments, you will understand the difficulties that developers faced over the past five years. You will also appreciate the excitement over Microsofts latest offering to the world the .NET platform. This platform means different things to different people. For the world of network administrators, it means new service applications, such as Microsoft Exchange 2000 Server, Microsoft SQL 2000 Server, and the soon-to-be-released Microsoft .NET Servers.

3

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

4For the development world, it means delivery of the long-awaited Visual Studio .NET and its arsenal of new and old languages, as well as the Common Language Runtime (CLR). This chapter will introduce you to the .NET platform and explore the fundamentals of the Visual C# programming language, as well as the introductory concepts of object-oriented programming (OOP). Please keep in mind that if you are an experienced OOP programmer, you may just need to skim this chapter for differences between C# and other languages, such as C++ and Java, or you may want to review the chapter as a refresher. Whatever route you choose, there is little or nothing in this chapter that will be directly tested on the Microsoft exams. However, having said that, Microsoft assumes a working knowledge of all the concepts covered in this chapter. If you are new to OOP, you may want to spend extra time in this chapter and work outside the book a little in order to bring your skills to the level required. This book assumes that you already have an excellent grasp of the concepts that are briefly looked at in this chapter.

Overview of .NETBefore one can start talking about the C# language, it is imperative to understand the platform upon which it works. Historically, programming using Microsoft development tools has involved the traditional Windows API, a programming interface that sits between coders and the desktop. Microsoft realized that as software progressed, APIs became complicated and corrupted by a series of updates and changes. Instead of continuing to improve upon it, Microsoft chose to completely revamp the process. .NET introduced a brand-new development platform, called the .NET Framework, which brings together many old, familiar languages, and, of course, includes a new and exciting language, Microsoft C# .NET.

So, What Is .NET?.NET is huge. Microsoft has invested a great portion of its budget to researching and developing this brand-new way of doing things. Its not just an offering to the programming world. It introduces a combined and consistent effort between operating systems, services, and programming languages. Of course, our focus will be on the .NET Framework, but in order to be conversant with the other side of .NET, here is the shortlist of elements that make up the .NET architecture: A set of .NET languages that can be said to function in a common environment These languages all execute in a common runtime and all make use of a common library of components. Any language can be said to be .NET-compliant if it does this. As the future unfolds, you will see many languages strive to function this way and join the ever-growing list of .NET languages. Visual Studio .NET includes a group of Microsoft-built languages that conform to the .NET standard. A set of services provided by the .NET Enterprise Servers Over the last few years, we have watched the release of a new e-mail server, Microsoft Exchange

Chapter 1: Introduction to C# .NET and the .NET Platform

5Server; a new database server, Microsoft SQL Server; and just recently, a whole new set of servers, such as BizTalk Server and Commerce Serverall 2000 releases. These servers provide the infrastructure that will be used to host and manage .NET applications and services. A distributed application service Increasingly, services will be accessible from the Internet, and Microsoft has been aware of this trend for a long time. As network bandwidth increases and waiting times decrease, it has become apparent that it is possible to provide real-time services through the Internet. The .NET platform makes use of XML (Extensible Markup Language) and SOAP (Simple Object Access Protocol) in order to describe and deliver data. A service for .NET enabled devices These devices include hand-held computers, cell phones, game machines, and so on. As you can see, .NET is more than just a development environment. However, the focus of this book will be just that. In the next sections, we will attempt to fight our way through the many acronyms and phrases that make up the usual descriptions of the .NET Framework. When we are done, we hope that you will have a new-found appreciation for the amount of time, work, and effort that went into making the .NET Framework the exciting offering that it is. You will also have a solid foundation for the topics that will be covered on the C# exams. PART I

Application Development in .NETQuite simply put, .NET (from a developers perspective) is a new runtime environment packaged together with a plethora of prebuilt, ready-to-use software components. Given that, we will start talking about things like the Common Language Runtime (CLR) and the .NET Framework. The CLR is the underlying software that allows your program to run, and the .NET Framework is a library or collection of object-oriented software built for you in order to make your programming tasks easier. Microsoft had several goals in mind when they decided to create the .NET platform: An object-oriented code execution environment provides a consistent approach. This means that whether you code for the Internet, the local desktop, or a remote, server-based distribution center, you will be accessing the same .NET runtime in order to execute your code. Fully object-oriented languages are built into the .NET platform. Familiar languages, such as Visual Basic, have been extended and reworked to make them more object-oriented. C# is the new language that combines elements of Java and C++. NOTE If you are unfamiliar with the concepts of object-oriented programming, we will explore programming with classes later in this chapter. Refer to Chapter 3 for some of the more advanced features.

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

6 In a world of Internet access and deepened security concerns, it is a challenge to securely execute code. The .NET platform includes several built-in mechanisms to ensure the code conforms to specifications, such as the Common Language Specifications (CLS). Code must also be guaranteed to be safe when it enters the runtime environment and, conversely, the user of the code must be authorized to use the program. We will be exploring many levels of security in the coming chapters. .NET provides developers with a heightened sense of code security. If you have coded using dynamic-link libraries (DLLs), you will appreciate it when we tell you that this nightmare is now over. Developers have long been concerned with versioning DLLs, which are reusable software components whose references are stored in the Windows Registry. By versioning, we mean that these components, when upgraded, have to provide some backward compatibility as well as performing their new functions in the upgraded environment. Microsoft has eliminated these concerns by replacing the traditional DLL with assemblies. Versioning is built right into the assembly, and many versions can live together harmoniously. An end user of application software is guaranteed, in this fast-paced technological world, to have the patience and the attention span of a hyperactive child. This is not a criticismthis is a fact. We have developed a world in which we dont want to wait for technology. To this end, the .NET Framework makes it possible to reduce the performance problems that we have experienced with languages that are scripted (VBScript, JavaScript, and the like) and languages that are interpreted at runtime (Java). As you read on in this book, you will gain an understanding of how this is accomplished and why this is such an important development. If you are not excited yet, keep reading! We will be showing you how easy it is to create applications that can be initiated from the Internet, as well as software that interacts with the desktop in a traditional forms manner. You will be able to create practical applications right away while you are preparing to take the Microsoft C# exams.

The .NET PlatformWeve mentioned the .NET platform so often that its now time to formalize our discussion. Just what is the .NET platform? In this section, we will explore the architecture of the .NET platform, which is made up of the .NET Framework, the Common Language Specifications (CLS), the Common Language Runtime (CLR), Microsoft Intermediate Language (MSIL), and the Base Class Library (BCL). Once weve finished with that al phabet soup, we will put it all in perspective and look at how all the pieces come together to create a development platform.

The .NET FrameworkThe .NET Framework is made up of the Common Language Runtime, the Base Class Library, and services that allow you to create Web applications (ASP.NET) and Windows applications (Windows forms). If we explore the architecture, we can see how this is all put together.

Chapter 1: Introduction to C# .NET and the .NET Platform

7Figure 1-1 shows the overall picture, demonstrating how the .NET languages adhere to the rules provided by the Common Language Specifications. These languages can all be used independently to create Web services, Web forms, or Windows forms, and can all be used with built-in data describers (XML) and data accessors (ADO.NET and SQL). Every component of the .NET Framework can take advantage of the large prebuilt library of classes called the Base Class Library. Once everything is put together, the code that is created is executed in the Common Language Runtime, which is similar to (but different from) the Java Virtual Machine (JVM). You can see a very large difference in just this one pointthe JVM is designed to accommodate a single language (Java); but the Common Language Runtime is designed to allow any .NET-compliant language to execute its code. At the time of writing, these languages included Visual Basic .NET, C# .NET, and C++ .NET, but any language could become .NET-compliant. The following sections will address each of the parts of the architecture. By understanding the parts, you will understand the sum of the partsthe excitement and functional power of .NET!

PART I

The CLSCommon Language SpecificationsIn an object-oriented environment, everything is an object. (This point is explained in this chapter and the more advanced features are explained in Chapter 3.) In a nutshell, you create a blueprint or template for an object (this is called the class file), and this class file is used to create multiple objects.TIP Consider a wheel. You may want to create many wheels in your lifetime; but each wheel will have certain characteristics and certain functions. For example, each wheel will have a specific circumference, width, and color. Each wheel must be able to be rotated and installed on a vehicle. So now, suppose your neighbor also wants to create a wheel. Why reinvent the wheel? (Pardon the bad joke.) You can create a template and share it with others. They create the physical wheels based on your template. This is the crux of object-oriented programmingthe template is the class file, and the physical wheels are the objects built from that class.

Figure 1-1 The .NET Framework architecture

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

8Once you have created an object, your object needs to communicate with many other objects. These other objects may have been created in another .NET language, but that doesnt matter, because each language adheres to the rules of the Common Language Specifications. The CLS defines such things as common variable types (this is called the Common Type SystemCTSyou knew there had to be an acronym!), common visibility (when and where can you see these variables), common method specifications, and so on. See how wonderful this is! Essentially, we are now all speaking the same language. You dont have one rule describing how C# composes its objects and another rule describing how Visual Basic does the same thing. To steal a phrase, there is now One rule to bind them all. One thing to note here is that the CLS simply provides the bare-bones rules. Languages can adhere to their own subset of the specification. In this case, the actual compilers do not need to be as powerful as those that support the full CLS. It also means that a language can add to the rules; however, if those languages interoperate with CLS-compliant code, they are not fully guaranteed to work. If you are interested in looking at the specifications, check them out at http://www.microsoft.com.

The CLRCommon Language RuntimeNot to harp on the point, but did you notice the similarity in names: Common Language Runtime and Common Language Specifications? Common to both is the word common. This is not just coincidence. The days of a language needing its own environment in which to execute its code are over. All .NET-compliant languages run in a common, managed runtime execution environment. Lets back up a step and explore the advantages of having a common runtime environment. Simplified development and integrated development with other languages As a programmer in the not-so-distant past, you had to concern yourself with such things as components written in different languages, GUIDs, something called IUnknown, and other nightmares. With the CLR, you can rely on code that is accessed from different languages. This is a huge benefit. One coder can write one module in C++, and another can access and use it from Visual Basic. Safe deployment and execution Security has been a concern of Microsoft developers for many years. With the release of the .NET Framework, Microsoft has demonstrated that it has listened to these concerns and made security a top-level priority. As soon as a class is loaded into the CLR, security begins. Type safety is checked (Is it a legitimate type? Is it safe to use?), verification refuses to let an application access random memory locations, and credentials are checked, to name just a few of the security measures. .NET also introduces the concept of assemblies, in which all the pieces of the product are put together in a package that includes security information. We will be exploring security in great detail in this book, both for the Windows applications and Web applications.

Chapter 1: Introduction to C# .NET and the .NET Platform

9 Automatic object management C++ programmers will be relieved when they find out that the .NET languages take care of memory issues automatically. In the earlier OOP days, programmers had to consciously create memory space for their objects and then remember to destroy that object space when they were finished with the objects. If they did not take care of this, they created a leaky application that would eventually grab all of the systems memory resources and choke the machine. With .NET, Microsoft has followed Javas lead by making use of a garbage collector that goes through the memory heap periodically and removes those objects that are no longer in use. Replacement of DLLs with versioned assemblies DLL Hell is over! Long live assemblies.NET-packaged components. The CLR uses the version information that comes packaged in an assembly to make sure that the application will load the correct component. This was a nightmare for DLL programmers, even though COM (Component Object Model) was supposed to correct most of the problems. The issue here was when your less-than-perfect component needed upgrading. Suppose someone has an application that needs the first version of your DLL, but you have installed software that has overwritten the previous version with a newer version. All of a sudden the application will stop running, since it is no longer able to access the component that it needs. Read on in this book to learn more about assemblies and how they solve this problem. Improved performance and scalability Performance and scalability are not new issues and, despite the hype, they will always be an issue, no matter the environment. However, Microsoft has attempted to provide you with many tools to improve the performance of applications. This book will address many of these issues and provide you with tips and tricks to continue improving performance. This is just a short list of the advantages of the Common Language Runtime. If you are interested in more details on the .NET platform, be sure to explore the wide range of technical articles available to you through the Microsoft network. PART I

MSILMicrosoft Intermediate LanguageSo how can many different languages be brought together and executed together? Enter Microsoft Intermediate Language (MSIL) or, as its more commonly known, Intermediate Language (IL). In its simplest terms, IL is a programming language. Yes, Virginia, there still is an assembly-type language. If you wanted to, you could write IL directly, compile it, and run it. But why would you? Microsoft has provided you with higher-level languages, such as C#, that you can use. Lets look at the steps you follow to create, compile, and run a C# program. By exploring this, you will be able to see where the MSIL fits in and how it works with the .NET Framework. 1. Write the source code in C#. 2. Compile the source code using the C# compiler (csc.exe).

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

103. The compiler produces MSIL code. This code is similar to Javas byte code; however, it is not interpreted code. It is compiled code that includes just-in-time (JIT) compilation, meaning that there is a significant performance improvement over Javas byte code. Not only is there a performance improvement, but all of the .NET languages compile into MSIL code. Now, hopefully, you can see where the language interoperability comes in. The MSIL also includes metadata, which describes the types that are included in your code. This means that there is no need for type libraries or Interface Definition Language (IDL). Its all included in the metadata. 4. Before the code is executed, the MSIL must be converted into platform-specific code. The CLR includes something called a JIT compiler. The combination of the metadata and MSIL code is contained in a Portable Executable (PE) file. 5. The code is executed on a runtime host. The runtime host includes the CLR. Runtime hosts are ASP.NET, Microsoft Internet Explorer, the desktop shell, and so on. If this is very confusing, have a look at Figure 1-2, which attempts to put the flow into a picture that explains the process. Well go into more detail on the process of writing and running C# programs later in this chapter.

The BCLBase Class LibraryIncluded in the .NET Framework is the Base Class Library. The BCL is a runtime library that describes many classes that can be used in any software project. Essentially, this means that you have at your fingertips an arsenal of prebuilt blueprints to assist you in your programming effort. Remember Microsofts goal of reducing development time? That goal is addressed in part by providing the Base Class Library. Suppose you have to create an application that provides network communication between two computers. Your first step would be to create a socket, which is a combination of the IP address and a port number. So you would set about creating a class file (see the next section for more on creating a class file) that describes this process. In anticipation of this, Microsoft has created a Socket class within the Base Class Library. You simplyFigure 1-1 The process of creating a .NET program

Chapter 1: Introduction to C# .NET and the .NET Platform

11plug into its functionality and use a socket rather than creating the design for one. Appendix D gives a sampling of the Class Library. PART I

The .NET LanguagesAs we write this, the following languages are included in Visual Studio .NET: Microsoft C# .NET, the subject of this book. Microsoft VB .NET, the newest release of Visual Basic. Microsoft C++ .NET, an extended version of Visual C++ that supports the .NET Framework. Microsoft J++, for compatibility only. It is not upgraded to .NET. JScript .NET.

Microsoft C# .NETC# (pronounced C sharp) is the latest language in the C family of languages. C# is a modern, type-safe, simple, object-oriented language that inherits its features from the C, C++, and Java languages. Java developers will feel very much at home with C#, as will C++ developers. However, both C++ and Java developers will discover that many of the shortcomings of those languages have been eliminated in C#. You will not find pointer arithmetic or memory allocations, both of which have complicated the lives of C++ programmers for years. You will also find that the creation of the executable program is not as complicated or performance-draining as it is in the Java language. NOTE The C# language looks like Java, but beware; there are differences that will create problems for seasoned Java developers. One item that will stand out immediately is the declaration for the Main() method (lowercase m in Java, main()). C#, like Java, is a case-sensitive language. You will also find that some of the familiar identifiers are spelled with a different case. Microsoft submitted the C# language to ECMA (European Computer Manufacturers Association) for consideration as a standard, and ECMA released the standard for C# at the end of 2001. Now that C# is a public standard, other vendors can release C# compilers written against that standard. This should propel C# to the same wide use as the ANSI standards did for C++. Microsofts implementation of the standard is called Microsoft C# .NET (referred to as C# in this book). The remainder of this chapter will serve as an introductory overview of the C# language and of object-oriented concepts.EXAM TIP Remember that you are not likely to find any questions on the Microsoft exam that are directly answered in this chapter. If you are familiar with either Java or C++, you will want to skim through the rest of this chapter.

MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide

12Basic C# ConceptsIt has been the tradition since the first book on the C language to start any text introducing a computer language with a program that prints Hello World! on the screen. We will follow that traditionhere is the Hello World! program written in Microsoft Visual C# .NET:using System; class Hello { public static int Main() { Console.WriteLine("Hello World!"); return 0; } }

The program can be typed into any editor (we used Notepad) and saved in a file with a .cs extension (.cs is the default file extension for C#). To be able to run the Visual Studio .NET command-line programs (like csc.exe) you will have to have the environment properly configured. The easiest way is to start the command prompt from the batch file supplied with Visual Studio .NET, which you can find by selecting Start | Programs | Microsoft Visual Studio .NET 7.0 | Visual Studio .NET Tools | Visual Studio .NET Command Prompt. In Chapter 5, we will introduce the Visual Studio development environment, which makes your coding and testing much easier, but for the purpose of examining the details of coding, we will stick to Notepad for now. Its important to note that the C# language is case sensitive. You must type everything exactly as listed. In order to create an executable file for the Hello World! program, you will have to compile the code into IL. The C# compiler will do that for you from a command line just enter csc.exe Hello.cs. You may also want to work with the integrated development environment (IDE) for Visual Studio. If this is your first time working with a Visual Studio release, refer to Chapter 5 for assistance. NOTE For the complete syntax of the csc program, see Appendix C.

Lets have a quick look at the five lines of code (shown previously) that display Hello World! in a console window. If you are familiar with C++ or Java, the program should look fairly readable, but there are some gotchas that you should be aware of. The first line (using System;) tells the compiler and runtime system where to search for library entries. This is very similar to the Java import keyword.CAUTION The C++ #include statement is not the same as the C# using statement, since the using statement does not insert any code into the executable. The #include used in C/C++ actually reads the content of the include file into the source file.

Chapter 1: Introduction to C# .NET and the .NET Platform

13In C#, everything is a class. Keep this statement in mind as you create your programs in C#. In reality, you are not creating traditional programs. Instead, you are creating classesyoull get a complete explanation of class files in the Object-Oriented Programming Concepts section of this chapter. In our example, the line class Hello is the definition of a new class called Hello. Every executable program must have one and only one entry point (an entry point being the start of execution). In our program, the entry point is the third line: public static int Main(). The four parts of the method Main() will be explored later in this chapter. In order to print Hello World! on the screen, we use the System libraries (which were referred to by the using statement). All input and output is handled through these libraries. The object that we interact with is the Console and the method that prints data on the console is the WriteLine() method. The line Console.WriteLine("Hello World!"); calls the WriteLine() method of the Console class in the System library. Congratulations! You have just written your first C# program. If o