Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Learning object-oriented programming in C# 5.0
Nội dung xem thử
Mô tả chi tiết
Learning
Object-Oriented
Programming
in C# 5.0
B. M. Harwani
Cengage Learning PTR
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Learning Object-Oriented
Programming in C# 5.0
B. M. Harwani
Publisher and General Manager,
Cengage Learning PTR: Stacy L. Hiquet
Associate Director of Marketing:
Sarah Panella
Manager of Editorial Services:
Heather Talbot
Senior Marketing Manager:
Mark Hughes
Product Manager: Heather Hurley
Project/Copy Editor: Karen A. Gill
Technical Reviewer: Joshua Smith
Interior Layout Tech: MPS Limited
Cover Designer: Mike Tanamachi
Indexer: Valerie Haynes Perry
Proofreader: Megan Belanger
© 2015 Cengage Learning PTR.
CENGAGE and CENGAGE LEARNING are registered trademarks of Cengage
Learning, Inc., within the United States and certain other jurisdictions.
ALL RIGHTS RESERVED. No part of this work covered by the copyright
herein may be reproduced, transmitted, stored, or used in any form or by any
means graphic, electronic, or mechanical, including but not limited to
photocopying, recording, scanning, digitizing, taping, Web distribution,
information networks, or information storage and retrieval systems, except
as permitted under Section 107 or 108 of the 1976 United States Copyright
Act, without the prior written permission of the publisher.
For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706.
For permission to use material from this text or product, submit
all requests online at cengage.com/permissions.
Further permissions questions can be emailed to
Microsoft, Windows, and Internet Explorer are either registered trademarks
or trademarks of Microsoft Corporation in the United States and/or other
countries.
All other trademarks are the property of their respective owners.
All images © Cengage Learning unless otherwise noted.
Cover background art: © Eugene Sergeev/Shutterstock.com.
Library of Congress Control Number: 2014933750
ISBN-13: 978-1-285-85456-4
ISBN-10: 1-285-85456-X
Cengage Learning PTR
20 Channel Center Street
Boston, MA 02210
USA
Cengage Learning is a leading provider of customized learning solutions
with office locations around the globe, including Singapore, the United
Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at:
international.cengage.com/region.
Cengage Learning products are represented in Canada by Nelson
Education, Ltd.
For your lifelong learning solutions, visit cengageptr.com.
Visit our corporate website at cengage.com.
Printed in the United States of America
1 2 3 4 5 6 7 16 15 14
eISBN-10: 1-285-85457-8
Today, the Internet is the most powerful source of information
thanks to the amazing efforts of many, including the following:
n Vinton Cerf, American Internet pioneer, who is recognized
as the father of the Internet
n Robert Kahn, American engineer who, along with Vinton
Cerf, invented the Transmission Control Protocol (TCP)
and the Internet Protocol (IP), the communication
protocols used on the Internet
n Tim Berners-Lee, British computer scientist also known
as the inventor of the World Wide Web (WWW)
I dedicate this book to them and to my mother, Nita Harwani.
The person I am today is a product of the moral
values she taught me.
Acknowledgments
I am grateful to Heather Hurley, product manager at Cengage Learning, for believing
in me and giving me an opportunity to create this work.
Thank you to Karen Gill who, as my project editor and copy editor, offered a significant amount of feedback that improved the chapters. She did first-class structural
and language editing and played a vital role in improving the quality of information. I appreciate her efforts in enhancing the contents of this book and giving it a
polished look.
I must thank Joshua Smith, the technical editor, for his excellent, detailed reviewing of
the work and the many helpful comments and suggestions he made.
A great big thank-you to the editorial and production staff and the entire team at
Cengage Learning who worked tirelessly to produce this book. I enjoyed working with
each of you. Thanks, too, to MPS Limited for making the book look good on pages.
I also want to mention my family, my small world. To my wife, Anushka, and my
wonderful children, Chirag and Naman, thank you for inspiring and motivating me
and above all for forgiving me for spending so much time on my computer.
Speaking of encouragement, I must mention my dear students, who have been a good
teacher for me. Their interesting problems and queries help me write books with a
practical approach.
iv
About the Author
B. M. Harwani is the founder and owner of Microchip Computer Education (MCE),
based in Ajmer, India, which provides computer education in all programming and
web developing platforms. He graduated with a BE in computer engineering from
the University of Pune, and he has a C Level diploma (Master’s diploma in computer
technology) from DOEACC, Government of India. As a teacher for more than
20 years, he has developed the art of explaining even the most complicated topics in
a straightforward and easily understandable fashion. His published books include
Foundation Joomla published by FriendsOfED, jQuery Recipes published by Apress,
Core Data iOS Essentials published by Packt, Introduction to Python Programming
and Developing GUI Applications with PyQT published by Cengage Learning, Android
Programming Unleashed published by Sams Publishing, The Android Tablet
Developer’s Cookbook (Developer’s Library) published by Addison-Wesley Professional, and UNIX & Shell Programming published by Oxford University Press, USA.
To learn more, visit his blog at http://bmharwani.com/blog.
v
Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiv
Chapter 1 The .NET Framework . . . ..................................1
C# Introduction....................................................1
.NET Framework Structure ...........................................2
Common Language Runtime (CLR) ................................. 4
Common Type System ........................................... 5
Common Language Specification .................................. 5
Assembly Use .....................................................5
Visual Studio IDE Overview ..........................................7
Visual Studio Installation ............................................8
Running a C# Program ..............................................9
Using the Visual Studio IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Using the Command-Line Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Line-by-Line Explanation of Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Modification of the Main( ) Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Chapter 2 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Identifiers and Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Predefined Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Predefined Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
vi
Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Floating-Point Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Decimal Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Character Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Bool Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Nullable Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Predefined Reference Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Implicitly Typed Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Implicit Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Explicit Type Conversion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Boxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Unboxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
The is Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The as Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Methods Defined by stream Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The Predefined Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Console I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Sending Output to the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Reading Input from the Console . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Comment Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Windows Form Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Chapter 3 Operators and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Adding Parentheses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
The typeof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
The sizeof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Checked and Unchecked Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Sign Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Equality Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Logical Boolean Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Logical Bitwise Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Shift Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Compound Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Contents vii
Ternary Operator (Conditional Operator) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
The Math Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Chapter 4 Decision-Making and Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The if-else-if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The switch Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
The do-while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The continue Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Chapter 5 Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
One-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Using Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Passing Command-Line Arguments to Main( ) . . . . . . . . . . . . . . . . . . . . . . . . . 135
Using StringBuilder. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Using ArrayList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Chapter 6 Methods and Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Return Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Method Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Optional and Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
C# Parameter Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Passing By Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Passing By Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Using Output Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Sending Variable Length Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
viii Contents
Your Own Type: Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Using Properties with Struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Defining Arrays in Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Using an Array of Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Nested Structs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Date and Time Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Using the DateTime Properties and Methods . . . . . . . . . . . . . . . . . . . . . . . 179
Using TimeSpan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Formatting Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Custom Date and Time Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Standard Date and Time Formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Chapter 7 Classes and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
The new Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Public Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Difference Between a Struct and a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Methods Defined in a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Private Members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Parameter Passing by Value and Reference . . . . . . . . . . . . . . . . . . . . . . . . 201
Output Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Use of Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
The this Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Default Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Parameterized Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Copy Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Constructor Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Constructor Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Garbage Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Static Classes and Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Namespace Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Namespace Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Namespace Alias Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Internal Modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Contents ix
Chapter 8 Inheritance and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Types of Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Single Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Multilevel Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Hierarchical Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Hybrid Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Method Overriding. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Sealed Class and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Method Hiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Abstract Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Abstract Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Chapter 9 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Invoking Interface Members at the Object Level . . . . . . . . . . . . . . . . . . . . . . . 286
Implementing Multiple Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Handling Ambiguity When Implementing Multiple Interfaces . . . . . . . . . . . . 290
Implementing an Interface Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Verifying Interface Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Using the as Keyword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Using the is Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Implementing a Multilevel Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Implementing Interface Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Understanding How a Structure Implements an Interface. . . . . . . . . . . . . . . . 300
Passing an Interface as a Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Returning an Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Chapter 10 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Binary Operator Overloading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Adding a Constant to an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Adding Two Complex Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Unary Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
true and false Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Comparison Operators Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Conversion Operators Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Creating an Implicit Conversion Operator. . . . . . . . . . . . . . . . . . . . . . . . . . 324
Creating an Explicit Conversion Operator. . . . . . . . . . . . . . . . . . . . . . . . . . 324
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
x Contents
Chapter 11 Delegates and Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Delegates Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Declaring Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Instantiating Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Invoking the Referenced Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Instance Versus Static Method Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Multicast Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Covariance and Contravariance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Generic Delegate Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Anonymous Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Difference Between Delegates and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . 352
Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Declaring an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Event Accessors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Chapter 12 Generics and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Standard Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
IEnumerators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Hashtable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
SortedList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
BitArray. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Generics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Generic Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Generic List<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Queue<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Stack<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
SortedSet. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
LinkedList<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Chapter 13 Managing Errors and Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . 409
Introducing Errors and Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
The Exception Hierarchy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Keywords Used in Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Using Multiple try Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Using Multiple catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Throwing an Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Contents xi
Nesting try Blocks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Creating Your Own Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Using the checked/unchecked Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Chapter 14 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Introduction to Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Using the Main Thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Creating Child Threads. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Life Cycle of a Thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Ensuring Exclusive Operations Using Monitors . . . . . . . . . . . . . . . . . . . . . . 447
Using the Lock Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Interrupting a Thread. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Chapter 15 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Introducing Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Handling Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Reading a File Randomly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Reading and Writing Primitive Types in Binary Format . . . . . . . . . . . . . . . . . . 465
Using BinaryWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Using BinaryReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Performing Character-Based File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Using StreamWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Implementing TextWriter. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Using StreamReader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Implementing TextReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Using StringReader and StringWriter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Reading an Entire File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Holding Data Temporarily Using MemoryStream. . . . . . . . . . . . . . . . . . . . . . . 480
Managing a Directory Using DirectoryInfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Chapter 16 ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Examining the Major Components of ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . 486
Connecting or Creating a Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Accessing Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498
Creating a Connection to the Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Opening the Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Creating a Command Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Closing the Connection and Releasing the Resources. . . . . . . . . . . . . . . . . 505
Displaying the Table Data Through GridView . . . . . . . . . . . . . . . . . . . . . . 510
xii Contents
Inserting Rows in a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Using Parameters with the Command Object. . . . . . . . . . . . . . . . . . . . . . . 521
Calling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Updating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532
Chapter 17 LINQ Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Implicitly Typed Local Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Object and Collection Initializers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535
Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536
Extension Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Anonymous Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
LINQ Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
LINQ to Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
LINQ to XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
LINQ to SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553
Chapter 18 Web-Based Application Development in .NET . . . . . . . . . . . . . . . 555
Examining the Visual Studio Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556
Building Your First Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Using the DropDownList Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570
Understanding the AutoPostBack Property . . . . . . . . . . . . . . . . . . . . . . . . 573
Using the ArrayList Class to Display Data in DropDownList . . . . . . . . . . . . 577
Using the CheckBox Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
Using the RadioButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Using the ListBox Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589
Using the LinkButton Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Using the HyperLink Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Using the ImageButton Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Working with WCF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
Creating a Client Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619
Chapter 19 .NET Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Using Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Private Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Shared Assemblies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Creating and Using a Shared Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .633
Contents xiii
Introduction
This book provides a thorough guide to the most valuable features of C# 5.0. It
explains object-oriented programming (OOP) principles and their benefit in developing real-world applications, and it integrates in-depth OOP fundamentals when developing code in C#. It provides essential, straightforward information to help readers
master the core capabilities of Visual C# 2013 and explores creating and using advanced
class features such as generics, collections, and operator overloading. Readers will dig
deep into web services, LINQ, multiple threading, and security features. This book also
offers complete coverage of .NET features, including assemblies, interfaces, delegates,
events, web application development, and ADO.NET.
What You’ll Find in This Book
This book focuses on hands-on projects. It successfully presents readers with a comprehensive guide covering each major skill required for developing apps using C# 5.0.
Here’s what you’ll find in this solid, comprehensive reference:
n Plenty of running examples
n A progression from easy examples to deeper, complex topics
n An explanation of object-oriented programming principles and their benefit in
developing real-world applications
n Coverage of inheritance, polymorphism, interfaces, operator overloading,
delegates, and events
xiv