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 Chapter 2: Living with Variability -- Declaring 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 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 Chapter 4: Smooth Operators 81 Performing Arithmetic 81 Simple operators 82 Operating orders 82 The assignment operator 84 The increment operator 84 Performing Logical Comparisons -- Is That Logical? 85 Comparing floating-point numbers: Is your float bigger than mine? 86 Compounding the confusion with compound logical operations 87 Matching Expression Types at TrackDownAMate.com 89 Calculating the type of an operation 89 Assigning types 91 Changing how an operator works: Operator overloading 92 Chapter 5: Getting into the Program Flow 95 Branching Out with if and switch 96 Introducing the if statement 97 Examining the else statement 100 Avoiding even the else 101 Nesting if statements 102 Running the switchboard 104 Here We Go Loop-the-Loop 110 Looping for a while 111 Doing the do while loop 114 Breaking up is easy to do 115 Looping until you get it right 116 Focusing on scope rules 120 Looping a Specified Number of Times with for 120 A for loop example 121 Why do you need another loop? 122 Nesting loops 123 Chapter 6: Lining Up Your Ducks with Collections 125 The C# Array 126 The argument for the array 126 The fixed-value array 127 The variable-length array 129 Initializing an array 132 Processing Arrays by Using foreach 133 Working with foreach loops in a standard way 133 Relying on GetEnumerator support 134 Sorting Arrays of Data 136 Using var for Arrays 139 Loosening Up with C# Collections 140 Understanding Collection Syntax 141 Figuring out 142 Going generic 142 Using Lists 143 Instantiating an empty list 143 Creating a list of type int 144 Converting between lists and arrays 144 Searching lists 144 Performing other list tasks 145 Using Dictionaries 145 Creating a dictionary 145 Searching a dictionary 146 Iterating a dictionary 146 Array and Collection Initializers 147 Initializing arrays 148 Initializing collections 148 Using Sets 149 Performing special set tasks 149 Creating a set 150 Adding items to a set 150 Performing a union 151 Performing an intersection 152 Performing a difference 153 Chapter 7: Stepping through Collections 155 Iterating through a Directory of Files 156 Using the LoopThroughFiles program 156 Getting started 157 Obtaining the initial input 157 Creating a list of files 159 Formatting the output lines 160 Displaying the hexadecimal output 161 Running from inside Visual Studio 163 Iterating foreach Collections: Iterators 164 Accessing a collection: The general problem 164 Letting C# access data foreach container 167 Accessing Collections the Array Way: Indexers 169 Indexer format 169 An indexer program example 170 Looping Around the Iterator Block 173 Creating the required iterator block framework 174 Iterating days of the month: A first example 176 What a collection is, really 177 Iterator syntax gives up so easily 178 Iterator blocks of all shapes and sizes 180 Chapter 8: Buying Generic 187 Writing a New Prescription: Generics 188 Generics are type-safe 188 Generics are efficient 189 Classy Generics: Writing Your Own 190 Shipping packages at OOPs 190 Queuing at OOPs: PriorityQueue 191 Unwrapping the package 194 Touring Main() 196 Writing generic code the easy way 197 Saving PriorityQueue for last 198 Using a (nongeneric) Simple Factory class 201 Understanding Variance in Generics 205 Contravariance 206 Covariance 208 Chapter 9: Some Exceptional Exceptions 209 Using an Exceptional Error-Reporting Mechanism 210 About try blocks 211 About catch blocks 211 About finally blocks 212 What happens when an exception is thrown 213 Throwing Exceptions Yourself 215 Can I Get an Exceptional Example? 216 Working with Custom Exceptions 220 Planning Your Exception-Handling Strategy 221 Some questions to guide your planning 221 Guidelines for code that handles errors well 222 How to find out which methods throw which exceptions 223 Grabbing Your Last Chance to Catch an Exception 225 Throwing Expressions 226 Chapter 10: Creating Lists of Items with Enumerations 229 Seeing Enumerations in the Real World 230 Working with Enumerations 231 Using the enum keyword 231 Creating enumerations with initializers 233 Specifying an enumeration data type 234 Creating Enumerated Flags 235 Defining Enumerated Switches 237 Working with Enumeration Methods 238 Book 2: Object-Oriented C# Programming 241 Chapter 1: Showing Some Class 243 A Quick Overview of Object-Oriented Programming 244 Considering OOP basics 244 Extending classes to meet other needs 244 Keeping objects safe 245 Working with objects 246 Defining a Class and an Object 246 Defining a class 247 What''s the object? 249 Accessing the Members of an Object 250 Working with Object-Based Code 250 Using the traditional approach 250 Using the C# 9.0 approach 252 Discriminating between Objects 253 Can You Give Me References? 254 Classes That Contain Classes Are the Happiest Classes in the World 256 Generating Static in Class Members 257 Defining const and readonly Data Members 259 Chapter 2: We Have Our Methods 261 Defining and Using a Method 262 Method Examples for Your Files 263 Understanding the problem 264 Working with standard coding methods 265 Applying a refactoring approach 268 Working with local functions 271 Having Arguments with Methods 273 Passing an argument to a method 273 Passing multiple arguments to methods 274 Matching argument definitions with usage 276 Overloading a method doesn''t mean giving it too much to do 276 Implementing default arguments 278 Using the Call-by-Reference Feature 280 Defining a Method with No Re.
C# 10. 0 All-In-One for Dummies