ebook img

Beginning C# 7 Programming with Visual Studio 2017 PDF

887 Pages·2018·11.44 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Beginning C# 7 Programming with Visual Studio 2017

BEGINNING C# 7 Programming with Visual Studio 2017 Benjamin Perkins Jacob Vibe Hammer Jon D. Reid Beginning C# 7 Programming with Visual Studio® 2017 Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2018 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-119-45868-5 ISBN: 978-1-119-45872-2 (ebk) ISBN: 978-1-119-45866-1 (ebk) Manufactured in the United States of America CONTENTS INTRODUCTION xxi ParT I: THE C# LaNGUaGE CHaPTEr 1: INTrODUCING C# 3 What Is the .NET Framework? 4 What’s in the .NET Framework? 4 .NET Standard and .NET Core 5 Writing Applications Using the .NET Framework and .NET Core 5 CIL and JIT 6 Assemblies 7 Managed Code 7 Garbage Collection 7 Fitting It Together 8 Linking 9 What Is C#? 9 Applications You Can Write with C# 10 C# in this Book 11 Visual Studio 2017 11 Visual Studio 2017 Products 11 Solutions 12 CHaPTEr 2: WrITING a C# PrOGraM 15 The Visual Studio 2017 Development Environment 16 Console Applications 21 The Solution Explorer 24 The Properties Window 25 The Error List Window 25 Desktop Applications 26 CHaPTEr 3: VarIaBLES aND EXPrESSIONS 33 Basic C# Syntax 34 Basic C# Console Application Structure 36 Variables 38 Simple Types 38 Variable Naming 43 Literal Values 43 Binary Literals and Digit Separators 44 String Literals 45 Expressions 46 Mathematical Operators 47 Assignment Operators 52 Operator Precedence 53 Namespaces 54 CHaPTEr 4: FLOW CONTrOL 59 Boolean Logic 60 Boolean Bitwise and Assignment Operators 62 Operator Precedence Updated 64 Branching 65 The Ternary Operator 65 The if Statement 65 Checking More Conditions Using if Statements 68 The switch Statement 69 Looping 72 do Loops 73 while Loops 75 for Loops 77 Interrupting Loops 78 Infinite Loops 79 CHaPTEr 5: MOrE aBOUT VarIaBLES 83 Type Conversion 84 Implicit Conversions 84 Explicit Conversions 86 Explicit Conversions Using the Convert Commands 88 Complex Variable Types 91 Enumerations 91 Defining Enumerations 92 Structs 96 Defining Structs 96 Arrays 99 Declaring Arrays 99 foreach Loops 102 Pattern Matching with switch case expression 102 Multidimensional Arrays 106 Arrays of Arrays 108 String Manipulation 109 CHaPTEr 6: FUNCTIONS 117 Defining and Using Functions 118 Return Values 120 Parameters 122 Parameter Matching 124 Parameter Arrays 124 Reference and Value Parameters 126 Out Parameters 129 Tuples 130 Variable Scope 131 Variable Scope in Other Structures 134 Parameters and Return Values versus Global Data 136 Local Functions 137 The Main() Function 138 Struct Functions 141 Overloading Functions 142 Using Delegates 144 CHaPTEr 7: DEBUGGING aND ErrOr HaNDLING 149 Debugging in Visual Studio 150 Debugging in Nonbreak (Normal) Mode 150 Outputting Debugging Information 151 Tracepoints 156 Diagnostics Output Versus Tracepoints 158 Debugging in Break Mode 158 Entering Break Mode 158 Monitoring Variable Content 162 Stepping through Code 164 Immediate and Command Windows 166 The Call Stack Window 167 Error Handling 167 try…catch…finally 168 Throw Expressions 175 Listing and Configuring Exceptions 176 CHaPTEr 8: INTrODUCTION TO OBJECT-OrIENTED PrOGraMMING 179 What Is Object-Oriented Programming? 180 What Is an Object? 181 Properties and Fields 182 Methods 183 Everything’s an Object 184 The Life Cycle of an Object 184 Constructors 184 Destructors 185 Static and Instance Class Members 185 Static Constructors 185 Static Classes 186 OOP Techniques 186 Interfaces 187 Disposable Objects 188 Inheritance 188 Polymorphism 191 Interface Polymorphism 192 Relationships between Objects 193 Containment 193 Collections 194 Operator Overloading 194 Events 195 Reference Types versus Value Types 195 OOP in Desktop Applications 196 CHaPTEr 9: DEFINING CLaSSES 203 Class Definitions in C# 204 Interface Definitions 206 System.Object 209 Constructors and Destructors 211 Constructor Execution Sequence 212 OOP Tools in Visual Studio 216 The Class View Window 216 The Object Browser 218 Adding Classes 219 Class Diagrams 220 Class Library Projects 222 Interfaces Versus Abstract Classes 226 Struct Types 228 Shallow Copying Versus Deep Copying 230 CHaPTEr 10: DEFINING CLaSS MEMBErS 233 Member Definitions 234 Defining Fields 234 Defining Methods 235 Defining Properties 236 Tuple Deconstruction 241 Refactoring Members 242 Automatic Properties 243 Additional Class Member Topics 244 Hiding Base Class Methods 244 Calling Overridden or Hidden Base Class Methods 246 The this Keyword 246 Using Nested Type Definitions 247 Interface Implementation 249 Implementing Interfaces in Classes 250 Explicit Interface Member Implementation 251 Additional Property Accessors 252 Partial Class Definitions 252 Partial Method Definitions 253 Example Application 255 Planning the Application 255 The Card Class 255 The Deck Class 255 Writing the Class Library 256 Adding the Suit and Rank Enumerations 257 Adding the Card Class 259 Adding the Deck Class 260 A Client Application for the Class Library 263 The Call Hierarchy Window 265 CHaPTEr 11: COLLECTIONS, COMParISONS, aND CONVErSIONS 269 Collections 270 Using Collections 271 Defining Collections 276 Indexers 277 Adding a Cards Collection to CardLib 279 Keyed Collections and IDictionary 282 Iterators 283 Iterators and Collections 288 Deep Copying 289 Adding Deep Copying to CardLib 290 Comparisons 292 Type Comparisons 292 Boxing and Unboxing 292 The is Operator 294 Pattern Matching with the is Operator Pattern Expression 297 Value Comparisons 298 Operator Overloading 298 Adding Operator Overloads to CardLib 302 The IComparable and IComparer Interfaces 308 Sorting Collections 309 Conversions 313 Overloading Conversion Operators 313 The as Operator 315 CHaPTEr 12: GENErICS 319 What Are Generics? 320 Using Generics 321 Nullable Types 321 Operators and Nullable Types 322 The ?? Operator 323 The ?. Operator 324 Working with Nullable Types 325 The System.Collections.Generic Namespace 329 List<T> 330 Sorting and Searching Generic Lists 331 Dictionary<K, V> 337 Modifying CardLib to Use a Generic Collection Class 339 Defining Generic Types 339 Defining Generic Classes 340 The default Keyword 342 Constraining Types 342 Inheriting from Generic Classes 348 Generic Operators 349 Generic Structs 350 Defining Generic Interfaces 350 Defining Generic Methods 351 Defining Generic Delegates 352 Variance 353 Covariance 354 Contravariance 354 CHaPTEr 13: aDDITIONaL C# TECHNIQUES 359 The :: Operator and the Global Namespace Qualifier 360 Custom Exceptions 361 Adding Custom Exceptions to CardLib 362 Events 363 What Is an Event? 363 Handling Events 365 Defining Events 368 Multipurpose Event Handlers 371 The EventHandler and Generic EventHandler<T> Types 374 Return Values and Event Handlers 374 Anonymous Methods 375 Expanding and Using CardLib 375 Attributes 384 Reading Attributes 384 Creating Attributes 385 Initializers 386 Object Initializers 387 Collection Initializers 389 Type Inference 392 Anonymous Types 394 Dynamic Lookup 398 The dynamic Type 399 Advanced Method Parameters 402 Optional Parameters 403 Optional Parameter Values 404 The OptionalAttribute Attribute 404 Optional Parameter Order 404 Named Parameters 404 Lambda Expressions 409 Anonymous Methods Recap 409 Lambda Expressions for Anonymous Methods 410 Lambda Expression Parameters 413 Lambda Expression Statement Bodies 414 Lambda Expressions as Delegates and Expression Trees 415 Lambda Expressions and Collections 416 ParT II: WINDOWS PrOGraMMING CHaPTEr 14: BaSIC DESKTOP PrOGraMMING 425 XAML 426 Separation of Concerns 427 XAML in Action 427 Namespaces 428 Code-Behind Files 429 The Playground 429 WPF Controls 430 Properties 432 Dependency Properties 435 Attached Properties 435 Events 436 Handling Events 437 Routed Events 438 Routed Commands 438 Control Types 441 Control Layout 441 Basic Layout Concepts 441 Stack Order 442 Alignment, Margins, Padding, and Dimensions 442 Border 443 Visual Debugging Tools 443 Layout Panels 444 Canvas 444 DockPanel 446 StackPanel 448 WrapPanel 449 Grid 449 The Game Client 452 The About Window 453 Designing the User Interface 453 The Image Control 454 The Label Control 454 The TextBlock Control 454 The Button Control 455 The Options Window 458 The TextBox Control 459 The CheckBox Control 459 The RadioButton Control 460

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.