ebook img

C# 10.0 All-in-One For Dummies (For Dummies (Computer/Tech)) PDF

867 Pages·2022·17.067 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 C# 10.0 All-in-One For Dummies (For Dummies (Computer/Tech))

C# 10.0 A L L - I N - O N E C# 10.0 A L L - I N - O N E by John Paul Mueller C# 10.0 All-in-One For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2022 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada 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 the prior written permission of the Publisher. 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. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. 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. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHORS HAVE USED THEIR BEST EFFORTS IN PREPARING THIS WORK, THEY 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 ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES, WRITTEN SALES MATERIALS OR PROMOTIONAL STATEMENTS FOR THIS WORK. THE FACT THAT AN ORGANIZATION, WEBSITE, OR PRODUCT IS REFERRED TO IN THIS WORK AS A CITATION AND/ OR POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE PUBLISHER AND AUTHORS ENDORSE THE INFORMATION OR SERVICES THE ORGANIZATION, WEBSITE, OR PRODUCT MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING PROFESSIONAL SERVICES. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A SPECIALIST WHERE APPROPRIATE. FURTHER, READERS SHOULD BE AWARE THAT WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. NEITHER THE PUBLISHER NOR AUTHORS SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. 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: 2021951074 ISBN: 978-1-11-983907-1 (pbk) ISBN 978-1-11-984012-1 (ebk); ISBN 978-1-11-983908-8 (ebk) Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Book 1: The Basics of C# Programming . . . . . . . . . . . . . . . . . . . . . . . .5 C# 10.0 All-in-One CHAPTER 1: Creating Your First C# Console Application . . . . . . . . . . . . . . . . . . . . . . . . .7 CHAPTER 2: Living with Variability — Declaring Value-Type Variables . . . . . . . . . . . . 25 CHAPTER 3: Pulling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 CHAPTER 4: Smooth Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 CHAPTER 5: Getting into the Program Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 CHAPTER 6: Lining Up Your Ducks with Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 CHAPTER 7: Stepping through Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 CHAPTER 8: Buying Generic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 CHAPTER 9: Some Exceptional Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 CHAPTER 10: Creating Lists of Items with Enumerations . . . . . . . . . . . . . . . . . . . . . . . 229 Book 2: Object-Oriented C# Programming . . . . . . . . . . . . . . . . . . 241 CHAPTER 1: Showing Some Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 CHAPTER 2: We Have Our Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 CHAPTER 3: Let Me Say This about this . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 CHAPTER 4: Holding a Class Responsible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 CHAPTER 5: Inheritance: Is That All I Get? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 CHAPTER 6: Poly-what-ism? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353 CHAPTER 7: Interfacing with the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 CHAPTER 8: Delegating Those Important Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 CHAPTER 9: Can I Use Your Namespace in the Library? . . . . . . . . . . . . . . . . . . . . . . . 433 CHAPTER 10: Improving Productivity with Named and Optional Parameters . . . . . . 465 CHAPTER 11: Interacting with Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Book 3: Designing for C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 CHAPTER 1: Writing Secure Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 .99 CHAPTER 2: Accessing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509 CHAPTER 3: Fishing the File Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525 CHAPTER 4: Accessing the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 CHAPTER 5: Creating Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559 CHAPTER 6: Programming Dynamically! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 Book 4: A Tour of Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 CHAPTER 1: Getting Started with Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 CHAPTER 2: Using the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 CHAPTER 3: Customizing Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 Book 5: Windows Development with WPF . . . . . . . . . . . . . . . . . . 639 CHAPTER 1: Introducing WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 CHAPTER 2: Understanding the Basics of WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 CHAPTER 3: Data Binding in WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 CHAPTER 4: Practical WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 CHAPTER 5: Programming for Windows 10 and Above . . . . . . . . . . . . . . . . . . . . . . . . 721 Book 6: Web Development with ASP.NET . . . . . . . . . . . . . . . . . . . 743 CHAPTER 1: Creating a Basic ASP.NET Core App . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745 CHAPTER 2: Employing the Razor Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . 761 CHAPTER 3: Generating and Consuming Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795 Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 BOOK 1: THE BASICS OF C# PROGRAMMING . . . . . . . . . . . . . . . . 5 CHAPTER 1: Creating Your First C# Console Application . . . . . . . . . .7 Getting a Handle on Computer Languages, C#, and .NET . . . . . . . . . . . . 8 What’s a program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 What’s C#? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 What’s .NET? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 What is Visual Studio 2022? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Creating Your First Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Creating the source program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Taking it out for a test drive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Making Your Console App Do Something . . . . . . . . . . . . . . . . . . . . . . . . 17 Reviewing Your Console Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 The program framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 The meat of the program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Replacing All that Ceremonial Code: Top-Level Statements . . . . . . . . . 20 Introducing the Toolbox Trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Saving code in the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Reusing code from the Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Interacting with C# Online . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Working with Jupyter Notebook: The Short Version . . . . . . . . . . . . . . . .23 Living with Variability — Declaring CHAPTER 2: Value-Type Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Declaring a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 What’s an int? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Rules for declaring variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Variations on a theme: Different types of int . . . . . . . . . . . . . . . . . . 28 Representing Fractions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Handling Floating-Point Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Declaring a floating-point variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Examining some limitations of floating-point variables . . . . . . . . . 32 Table of Contents vii Using the Decimal Type: Is It an Integer or a Float? . . . . . . . . . . . . . . . . 34 Declaring a decimal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Comparing decimals, integers, and floating-point types . . . . . . . . . 35 Examining the bool Type: Is It Logical? . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Checking Out Character Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 The char variable type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Special chars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 The string type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 What’s a Value Type? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Comparing string and char . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Calculating Leap Years: DateTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Declaring Numeric Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Changing Types: The Cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Letting the C# Compiler Infer Data Types . . . . . . . . . . . . . . . . . . . . . . . . 46 CHAPTER 3: Pulling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 The Union Is Indivisible, and So Are Strings . . . . . . . . . . . . . . . . . . . . . . 50 Performing Common Operations on a String . . . . . . . . . . . . . . . . . . . . . 51 Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Equality for all strings: The Compare() method . . . . . . . . . . . . . . . . 52 Would you like your compares with or without case? . . . . . . . . . . . 56 What If I Want to Switch Case? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Distinguishing between all-uppercase and all-lowercase strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Converting a string to upper- or lowercase . . . . . . . . . . . . . . . . . . . . 57 Looping through a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Searching Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Can I find it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Is my string empty? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Using advanced pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Getting Input from Users in Console Applications . . . . . . . . . . . . . . . . . 61 Trimming excess white space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Parsing numeric input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62 Handling a series of numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Joining an array of strings into one string . . . . . . . . . . . . . . . . . . . . . 66 Controlling Output Manually . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Using the Trim() and Pad() methods . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Using the Concatenate() method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Go Ahead and Split() that concatenate program . . . . . . . . . . . . . . . 71 Formatting Your Strings Precisely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Using the String.Format() method . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Using the interpolation method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 StringBuilder: Manipulating Strings More Efficiently . . . . . . . . . . . . . . . 77 viii C# 10.0 All-in-One For Dummies

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.