MCSD Certification Toolkit (Exam 70-483): Programming in C#
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 9781118612095
ISBN: 978-1-118-61206-4 (ebk)
ISBN: 978-1-118-72950-2 (ebk)
ISBN: 978-1-118-72929-8 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2013933931
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
To Lia for her patience and understanding during the writing process. All my love.
For Maki.
To Renee, Madison, and Courtney.
Tiberiu Covaci is an Independent trainer and mentor teaching C# and .NET in general, and ASP.NET and parallel computing in particular. He works closely with Microsoft Learning helping them develop new courses, conducting beta classes and doing technology reviews for the upcoming courses. He was part of the Microsoft Certified Trainer Advisory Council between 2010 and 2012.
Tiberiu is a popular speaker at industry conferences and user groups around the world. His sessions and workshops get good reviews from both the attendees and the organizers.
Tiberiu is a Microsoft Certified Trainer and holds almost all .NET certification from .NET 2.0 and forward. He is as well an IASA certified trainer, an ASP.NET Insider, and a Telerik Insider. He is an INETA Speaker Bureau member and IASA Speaker. For his dedication and passion, Microsoft and Telerik presented Tibi with the MVP Award.
Tiberiu is the husband of lovely Nicoleta and the proud father of Anna and Disa.
Gerry O’Brien currently works at Microsoft as a program manager in Microsoft Learning where he manages internal tools and platforms working with teams of developers and testers. Prior to the program manager role, Gerry worked as the Certification Product Planner for the developer and SQL Server audiences at Microsoft Learning. In that role, he planned the exam portfolio for these audiences, working with industry experts to define the exam content and manage the exam from envisioning through development, beta, and release. Prior to working at Microsoft, Gerry worked as a software development consultant and trainer.
Rod Stephens started out as a mathematician, but, while studying at MIT, discovered how much fun programming is and has been programming professionally ever since. During his career, he has worked on an eclectic assortment of applications in such fields as telephone switching, billing, repair dispatching, tax processing, wastewater treatment, concert ticket sales, cartography, and training for professional football players.
Rod is a Microsoft Visual Basic Most Valuable Professional (MVP) and has taught introductory programming at ITT Technical Institute. He has written more than two dozen books that have been translated into languages from all over the world, and more than 250 magazine articles covering Visual Basic, C#, Visual Basic for Applications, Delphi, and Java.
Rod’s popular VB Helper website (www.vb-helper.com) receives several million hits per month and contains thousands of pages of tips, tricks, and example programs for Visual Basic programmers, as well as example code for this book. His C# Helper website (www.csharphelper.com) contains similar material for C# programmers.
You can contact Rod at RodStephens@csharphelper.com or RodStephens@vb-helper.com.
Vince Varallo has been developing applications using Microsoft technologies for the past 17 years. He began his career as a Visual Basic 3 developer and has worked with VB 4, 5, and 6 until the .NET Framework 1.0 was released. He was an early adopter of ASP.NET and C#, and has concentrated on line-of-business applications throughout his entire career. He is currently the director of Technology Solutions at a digital marketing agency where he works with a wide variety of technologies. He previously authored ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008 and contributed ace Varallo has been developins an author for Professional Visual Basic 6: The 2003 Programmer’s Resource.
Anders Bratland combines his two passions, programming and teaching other people how to program, by working as a freelance consultant, which gives him the chance to work both as a Microsoft Certified Trainer and as a developer.
Anders is a well-known speaker at conferences like TechDays, Scandinavian Developer Conference, and Developer Summit. Anders is also active as speaker in different user groups, such as DotnetForum, and also as one of the organizers in the largest Swedish user group, Swenug.
Anders has a strong commitment to techniques and methods that can help projects to be successful, especially by adopting agile values and disciplines.
Anders is a Microsoft ASP.NET MVP and a member of the Swedish Microsoft Extended Expert Team, MEET.
Executive Editor: Robert Elliott
Project Editor: Jennifer Lynn
Technical Editor: Anders Bratland
Production Editor: Daniel Scribner
Copy Editor: San Dee Phillips
Editorial Manager: Mary Beth Wakefield
Freelancer Editorial Manager: Rosemarie Graham
Associate Director of Marketing: David Mayhew
Marketing Manager: Ashley Zurcher
Business Manager: Amy Knies
Production Manager: Tim Tate
Vice President and Executive Group Publishe: rRichard Swadley
Vice President and Executive Publisher: Neil Edde
Associate Publisher: Jim Minatel
Project Coordinator, Cover: Katie Crocker
Compositor: Jeff Lytle, Happenstance Type-O-Rama
Proofreader: James Saturnio, Word One
Indexer: Ron Strauss
Cover Designer: Wiley
Cover Image: iStockphoto/microstocker
I would like to thank my lovely wife Nicoleta and my daughters, Anna and Disa, for supporting me and putting up with me for the past three years. I know this was a long process, and I know you might have not liked it at times, but now that is done I hope that people will find it educational and then all of it was worth it.
I want to thank Bob Elliott for believing in me even when I didn’t. I want to thank Jennifer Lynn and Rosemarie Graham for their help in making this book happen. I would like to thank Anders Bratland for lending his expertise and making sure that this book is technologically accurate. I would like to thank my co-authors Gerry O’Brien, Rod Stephens, and Vince Varallo for their hard work and devotion.
I would also like to thank Sergiu Damian for his help reviewing my chapters, Catalin Pop for helping me with his expertise on encryption, and Susan Ibach and Christopher Harrison for recommending me as author.
Last but not least I would like to thank my parents for making me who I am.
—Tiberiu Covaci
When we first talked about writing this book, our idea was to offer a way to our readers to learn to program using C#, and the byproduct of this process was for you to pass the 70-483 certification exam given by Microsoft. Being certified on specific technologies helps you in many ways. First, it helps you understand which parts are considered by the specialists to be important. Second, it helps you to understand a new technology by having a goal. Finally, it helps you in your career because certifications are recognized by employers, and this can give you advantage over other applicants.
Microsoft recommends that you have at least 1 year of experience programming in C# before attempting to take Exam 70-483. In addition, we recommend that you have some experience with other programming languages, although it is not necessary. If you are an experienced programmer, we recommend you to skim the chapters you are familiar with and read in detail those chapters you are not so confident about. If you are a novice programmer, we recommend you read the entire book, and make sure you understand all the chapter test questions and the study the Cheat Sheet at the end of every chapter.
This book covers C# language version 5.0 and .NET Framework version 4.5. We tried to cover all the skills measured by Exam 70-483, with each chapter focusing on specific key objectives. We provide, as well, many representative sample test questions that are similar to the ones used by Microsoft. You can find these questions at the end of every chapter.
Instead of following the test objectives as they were specified by Microsoft, this book follows a more natural approach to learning, where the knowledge base is built gradually.
In every chapter in this book you can find the following parts:
Following is a breakdown of each chapter’s focuses:
Chapter 1, “Introducing the Programming C# Certification Test”: This chapter introduces you to the Microsoft certification process and to the specifics of the 70-483 Programming in C# certification.
Chapter 2, “Basic Program Structure”: This chapter covers the topics necessary for you to be successful in understanding core functionality in the C# programming language. Key topics enable you to learn about statements in C#, both simple and complex. At the end of this chapter, you will understand how to create basic programs in C#.
Chapter 3, “Working with the Type System”: This chapter covers the type system in C#. You learn about value and reference types, how to define them, and how to use them. You also learn the basic concepts of object-oriented programming.
Chapter 4, “Using Types”: This chapter talks about how to work with types, convert between data types, and work with dynamic types. After that you explore different ways to work with strings.
Chapter 5, “Creating and Implementing Class Hierarchies”: This chapter continues the discussion about object-oriented programming (started in Chapter 3), and describes how to create class hierarchies and classes that implement common .NET interfaces. It also covers the object’s life cycle and how to handle unmanaged resources.
Chapter 6, “Working with Delegates, Events, and Exceptions”: This chapter continues the discussion started in Chapter 3 about the type system and talks about two special data types: exceptions and delegates. After that, it discusses how to work with delegates to create and use events.
Chapter 7, “Multithreading and Asynchronous Processing”: This chapter shows you how to improve the performance of your application by using threads, tasks, and the new asynchronous programming paradigm introduced in C# 5.0.
Chapter 8, “Creating and Using Types with Reflection, Custom Attributes, the CodeDOM, and Lambda Expressions”: Reflection is the capability to analyze code dynamically, read, modify, and even invoke behavior dynamically. You learn how to define metadata for your code by using Attribute
classes. You also learn how to create code generators using the CodeDOM. Finally, you learn how to query sets of data using expression- and method-based lambda expressions.
Chapter 9, “Working with Data”: This chapter looks at different ways to work with data sets. It discusses arrays, collections, and technologies such as ADO.NET, ADO.NET Entity Framework, and WCF Data Services and how to work with the I/O system.
Chapter 10, “Working with Language Integrated Query (LINQ)”: This chapter covers ways to query data by using the Language Integrated Query.
Chapter 11, “Input Validation, Debugging, and Instrumentation”: This chapter starts by talking about different ways to validate data input. After that it continues to talk about ways to debug and instrument applications to minimize the errors.
Chapter 12, “Using Encryption and Managing Assemblies”: This chapter covers two apparently unrelated technologies. First, you cover encryption to understand how to ensure data integrity and privacy. After that you cover ways to manage assemblies as deployment units.
To run the samples in the book, you need the following:
The source code for the samples is available for download from the Wrox website at www.wrox.com/remtitle.cgi?isbn=1118612094.
To help you get the most from the text and keep track of what's happening, we’ve used a number of conventions throughout the book.
As for styles in the text:
persistence.properties
We use a monofont type with no highlighting for most code examples.
We use bold to emphasize code that is particularly important in the present
context or to show changes from a previous code snippet.
As you work through the examples in this book, you may choose either to type in all the code manually or to use the source code files that accompany the book. All the source code used in this book is available for download at www.wrox.com. Specifically for this book, the code download is on the Download Code tab at www.wrox.com/remtitle.cgi?isbn=1118612094.
You can also search for the book at www.wrox.com
to find the code. Alternatively, you can go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx to see the code available for this book and all other Wrox books.
At the beginning of each chapter, you can find the location of the major code files for the chapter. Throughout each chapter, you can also find references to the names of code files as needed in listing titles and text.
Most of the code on www.wrox.com is compressed in a ZIP, RAR archive, or similar archive format appropriate to the platform. After you download the code, just decompress it with an appropriate compression tool.
In addition to the code, on the website you will also find the sample test questions and answers included in this book, as well as additional sample test questions and answers not included in this book to help you practice for the 70-483 certification exam.
We make every effort to ensure that there are no errors in the text or in the code. However, no one is perfect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faulty piece of code, we would be grateful for your feedback. By sending in errata, you may save another reader hours of frustration, and at the same time, you can help us provide even higher quality information.
To find the errata page for this book, go to www.wrox.com/remtitle.cgi?isbn=1118612094 and click the Errata link. On this page you can view all errata that has been submitted for this book and posted by Wrox editors.
If you don’t spot “your” error on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll check the information, and if appropriate post a message to the book’s errata page and fix the problem in subsequent editions of the book.
For author and peer discussion, join the P2P forums at http://p2p.wrox.com. The forums are a web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.
At http://p2p.wrox.com, you can find a number of different forums that can help you, not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:
After you join, you can post new messages and respond to messages other users post. You can read messages at any time on the web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe to This Forum icon by the forum name in the forum listing.
For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works, as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.