Acknowledgments |
|
xxi | |
About the author |
|
xxiii | |
Introduction |
|
xxv | |
|
PART I INTRODUCING MICROSOFT VISUAL C# AND MICROSOFT VISUAL STUDIO 2022 |
|
|
|
|
3 | (42) |
|
Writing your first C# program |
|
|
3 | (6) |
|
Beginning programming with the Visual Studio 2022 environment |
|
|
9 | (5) |
|
Writing your first program using Visual Studio 2022 |
|
|
14 | (7) |
|
|
21 | (2) |
|
Namespaces and assemblies |
|
|
23 | (1) |
|
|
24 | (1) |
|
Creating a graphical application |
|
|
24 | (13) |
|
Examining the Universal Windows Platform app |
|
|
37 | (3) |
|
Adding code to the graphical application |
|
|
40 | (3) |
|
|
43 | (1) |
|
|
44 | (1) |
|
Chapter 2 Working with variables, operators, and expressions |
|
|
45 | (28) |
|
|
45 | (1) |
|
|
46 | (1) |
|
|
46 | (2) |
|
|
48 | (1) |
|
|
48 | (1) |
|
|
48 | (1) |
|
Specifying numeric values |
|
|
49 | (1) |
|
Working with primitive data types |
|
|
50 | (1) |
|
Unassigned local variables |
|
|
51 | (1) |
|
Displaying primitive data type values |
|
|
51 | (8) |
|
Using arithmetic operators |
|
|
59 | (1) |
|
|
59 | (2) |
|
Examining arithmetic operators |
|
|
61 | (6) |
|
|
67 | (1) |
|
Using associativity to evaluate expressions |
|
|
68 | (1) |
|
Associativity and the assignment operator |
|
|
68 | (1) |
|
Incrementing and decrementing variables |
|
|
69 | (1) |
|
|
70 | (1) |
|
Declaring implicitly typed local variables |
|
|
70 | (1) |
|
|
71 | (1) |
|
|
72 | (1) |
|
Chapter 3 Writing methods and applying scope |
|
|
73 | (38) |
|
|
73 | (1) |
|
|
74 | (1) |
|
Returning data from a method |
|
|
75 | (1) |
|
Using expression-bodied methods |
|
|
76 | (1) |
|
|
77 | (1) |
|
Specifying the method call syntax |
|
|
77 | (3) |
|
Returning multiple values from a method |
|
|
80 | (2) |
|
|
82 | (1) |
|
|
83 | (1) |
|
|
83 | (1) |
|
|
84 | (1) |
|
|
85 | (4) |
|
Using the Visual Studio Debugger to step through methods |
|
|
89 | (7) |
|
|
96 | (1) |
|
|
97 | (3) |
|
Using optional parameters and named arguments |
|
|
100 | (2) |
|
Defining optional parameters |
|
|
102 | (1) |
|
|
102 | (1) |
|
Resolving ambiguities with optional parameters and named arguments |
|
|
103 | (5) |
|
|
108 | (1) |
|
|
109 | (2) |
|
Chapter 4 Using decision statements |
|
|
111 | (22) |
|
Declaring Boolean variables |
|
|
111 | (1) |
|
|
112 | (1) |
|
Understanding equality and relational operators |
|
|
112 | (1) |
|
Understanding conditional logical operators |
|
|
113 | (1) |
|
|
114 | (1) |
|
Summarizing operator precedence and associativity |
|
|
114 | (1) |
|
|
115 | (1) |
|
Using if statements to make decisions |
|
|
116 | (1) |
|
Understanding if statement syntax |
|
|
116 | (1) |
|
Using blocks to group statements |
|
|
117 | (1) |
|
|
118 | (6) |
|
|
124 | (1) |
|
Understanding switch statement syntax |
|
|
124 | (1) |
|
Following the switch statement rules |
|
|
125 | (4) |
|
Using switch expressions with pattern matching |
|
|
129 | (2) |
|
|
131 | (1) |
|
|
132 | (1) |
|
Chapter 5 Using compound assignment and iteration statements |
|
|
133 | (20) |
|
Using compound assignment operators |
|
|
133 | (2) |
|
|
135 | (5) |
|
|
140 | (2) |
|
|
142 | (9) |
|
|
151 | (1) |
|
|
151 | (2) |
|
Chapter 6 Managing errors and exceptions |
|
|
153 | (28) |
|
Trying code and catching exceptions |
|
|
154 | (1) |
|
|
155 | (1) |
|
Using multiple catch handlers |
|
|
156 | (1) |
|
Catching multiple exceptions |
|
|
157 | (1) |
|
|
158 | (5) |
|
|
163 | (2) |
|
Using checked and unchecked integer arithmetic |
|
|
165 | (1) |
|
Writing checked statements |
|
|
166 | (1) |
|
Writing checked expressions |
|
|
166 | (4) |
|
|
170 | (4) |
|
|
174 | (1) |
|
|
175 | (1) |
|
|
176 | (1) |
|
|
177 | (4) |
|
PART II UNDERSTANDING THE C# OBJECT MODEL |
|
|
|
Chapter 7 Creating and managing classes and objects |
|
|
181 | (24) |
|
Understanding classification |
|
|
181 | (1) |
|
The purpose of encapsulation |
|
|
182 | (1) |
|
Defining and using a class |
|
|
182 | (2) |
|
Controlling accessibility |
|
|
184 | (2) |
|
Working with constructors |
|
|
186 | (1) |
|
|
187 | (9) |
|
|
196 | (1) |
|
Understanding static methods and data |
|
|
197 | (1) |
|
|
198 | (1) |
|
Creating a static field by using the const keyword |
|
|
199 | (1) |
|
Understanding static classes |
|
|
199 | (1) |
|
|
200 | (2) |
|
|
202 | (1) |
|
|
203 | (1) |
|
|
204 | (1) |
|
Chapter 8 Understanding values and references |
|
|
205 | (26) |
|
Copying value type variables and classes |
|
|
205 | (6) |
|
Understanding null values and nullable types |
|
|
211 | (1) |
|
The null-conditional and null-coalescing operators |
|
|
212 | (1) |
|
|
213 | (1) |
|
Understanding the properties of nullable types |
|
|
214 | (1) |
|
Using ref and out parameters |
|
|
215 | (1) |
|
|
216 | (1) |
|
|
216 | (3) |
|
How computer memory is organized |
|
|
219 | (1) |
|
Using the stack and the heap |
|
|
220 | (1) |
|
|
221 | (1) |
|
|
222 | (1) |
|
|
222 | (2) |
|
|
224 | (1) |
|
|
224 | (1) |
|
|
225 | (1) |
|
The switch statement revisited |
|
|
225 | (4) |
|
|
229 | (1) |
|
|
229 | (2) |
|
Chapter 9 Creating value types with enumerations and structures |
|
|
231 | (20) |
|
Working with enumerations |
|
|
231 | (1) |
|
|
231 | (1) |
|
|
232 | (1) |
|
Choosing enumeration literal values |
|
|
233 | (1) |
|
Choosing an enumeration's underlying type |
|
|
233 | (3) |
|
|
236 | (1) |
|
|
237 | (1) |
|
Understanding differences between structures and classes |
|
|
238 | (2) |
|
Declaring structure variables |
|
|
240 | (1) |
|
Understanding structure initialization |
|
|
240 | (5) |
|
Copying structure variables |
|
|
245 | (3) |
|
|
248 | (1) |
|
|
249 | (2) |
|
|
251 | (26) |
|
Declaring array variables |
|
|
252 | (1) |
|
Creating an array instance |
|
|
252 | (1) |
|
Populating and using an array |
|
|
253 | (1) |
|
Creating an implicitly typed array |
|
|
254 | (1) |
|
Accessing an individual array element |
|
|
255 | (1) |
|
Accessing a series of array elements |
|
|
255 | (1) |
|
Iterating through an array |
|
|
256 | (1) |
|
Passing arrays as parameters or return values for a method |
|
|
257 | (2) |
|
|
259 | (1) |
|
Using multidimensional arrays |
|
|
260 | (1) |
|
|
261 | (11) |
|
Accessing arrays that contain value types |
|
|
272 | (3) |
|
|
275 | (1) |
|
|
275 | (2) |
|
Chapter 11 Understanding parameter arrays |
|
|
277 | (12) |
|
|
277 | (1) |
|
|
278 | (1) |
|
|
279 | (2) |
|
|
281 | (2) |
|
|
283 | (3) |
|
Comparing parameter arrays and optional parameters |
|
|
286 | (2) |
|
|
288 | (1) |
|
|
288 | (1) |
|
Chapter 12 Working with inheritance |
|
|
289 | (22) |
|
|
289 | (1) |
|
|
290 | (2) |
|
The System.Object class revisited |
|
|
292 | (1) |
|
Calling base-class constructors |
|
|
292 | (1) |
|
|
293 | (2) |
|
|
295 | (1) |
|
Declaring virtual methods |
|
|
296 | (1) |
|
Declaring override methods |
|
|
297 | (3) |
|
Understanding protected access |
|
|
300 | (5) |
|
Creating extension methods |
|
|
305 | (4) |
|
|
309 | (1) |
|
|
310 | (1) |
|
Chapter 13 Creating interfaces and defining abstract classes |
|
|
311 | (28) |
|
|
311 | (1) |
|
|
312 | (1) |
|
Implementing an interface |
|
|
313 | (1) |
|
Referencing a class through its interface |
|
|
314 | (1) |
|
Working with multiple interfaces |
|
|
315 | (1) |
|
Explicitly implementing an interface |
|
|
316 | (2) |
|
Handling versioning with interfaces |
|
|
318 | (1) |
|
|
319 | (1) |
|
Defining and using interfaces |
|
|
320 | (9) |
|
|
329 | (1) |
|
|
330 | (1) |
|
|
330 | (1) |
|
|
331 | (1) |
|
Implementing and using an abstract class |
|
|
331 | (6) |
|
|
337 | (1) |
|
|
338 | (1) |
|
Chapter 14 Using garbage collection and resource management |
|
|
339 | (26) |
|
The life and times of an object |
|
|
339 | (1) |
|
|
340 | (3) |
|
Why use the garbage collector? |
|
|
343 | (1) |
|
How does the garbage collector work? |
|
|
344 | (1) |
|
|
345 | (1) |
|
|
345 | (1) |
|
|
346 | (1) |
|
|
346 | (1) |
|
The using statement and the IDisposable interface |
|
|
347 | (2) |
|
Calling the Dispose method from a finalizer |
|
|
349 | (2) |
|
Implementing exception-safe disposal |
|
|
351 | (8) |
|
Handling asynchronous disposal |
|
|
359 | (2) |
|
|
361 | (1) |
|
|
361 | (4) |
|
PART III UNDERSTANDING THE C# OBJECT MODEL |
|
|
|
Chapter 15 Implementing properties to access fields |
|
|
365 | (30) |
|
Implementing encapsulation by using methods |
|
|
366 | (1) |
|
|
367 | (3) |
|
|
370 | (1) |
|
|
371 | (1) |
|
|
371 | (1) |
|
|
372 | (1) |
|
Understanding property restrictions |
|
|
373 | (2) |
|
Declaring interface properties |
|
|
375 | (1) |
|
Replacing methods with properties |
|
|
376 | (4) |
|
Pattern-matching with properties |
|
|
380 | (1) |
|
Generating automatic properties |
|
|
381 | (2) |
|
Initializing objects by using properties |
|
|
383 | (2) |
|
Automatic properties and immutability |
|
|
385 | (3) |
|
Using records with properties to implement lightweight structures |
|
|
388 | (4) |
|
|
392 | (1) |
|
|
393 | (2) |
|
Chapter 16 Handling binary data and using indexers |
|
|
395 | (18) |
|
|
395 | (1) |
|
|
396 | (1) |
|
|
397 | (1) |
|
Manipulating binary values |
|
|
397 | (1) |
|
Solving the same problems using indexers |
|
|
398 | (2) |
|
Understanding indexer accessors |
|
|
400 | (1) |
|
Comparing indexers and arrays |
|
|
401 | (2) |
|
|
403 | (1) |
|
Using indexers in a Windows application |
|
|
404 | (6) |
|
|
410 | (1) |
|
|
410 | (3) |
|
Chapter 17 Introducing generics |
|
|
413 | (32) |
|
The problem: Issues with the object type |
|
|
413 | (4) |
|
|
417 | (2) |
|
Generics vs. generalized classes |
|
|
419 | (1) |
|
|
419 | (1) |
|
|
420 | (1) |
|
The theory of binary trees |
|
|
420 | (3) |
|
Building a binary tree class by using generics |
|
|
423 | (10) |
|
Creating a generic method |
|
|
433 | (1) |
|
Defining a generic method to build a binary tree |
|
|
434 | (4) |
|
Variance and generic interfaces |
|
|
438 | (2) |
|
|
440 | (1) |
|
|
441 | (3) |
|
|
444 | (1) |
|
|
444 | (1) |
|
Chapter 18 Using collections |
|
|
445 | (24) |
|
What are collection classes? |
|
|
445 | (2) |
|
The List<T> collection class |
|
|
447 | (2) |
|
The LinkedList<T> collection class |
|
|
449 | (1) |
|
The Queue<T> collection class |
|
|
450 | (1) |
|
The PriorityQueue<TEIement, TPriority> collection class |
|
|
451 | (1) |
|
The Stack<T> collection class |
|
|
452 | (1) |
|
The Dictionary<TKey, TValue> collection class |
|
|
453 | (1) |
|
The SortedList<TKey, TValue> collection class |
|
|
454 | (1) |
|
The HashSet<T> collection class |
|
|
455 | (2) |
|
Using collection initializers |
|
|
457 | (1) |
|
Find methods, predicates, and lambda expressions |
|
|
458 | (2) |
|
The forms of lambda expressions |
|
|
460 | (1) |
|
Lambda expressions and anonymous methods |
|
|
461 | (1) |
|
Comparing arrays and collections |
|
|
462 | (4) |
|
|
466 | (1) |
|
|
467 | (2) |
|
Chapter 19 Enumerating collections |
|
|
469 | (14) |
|
Enumerating the elements in a collection |
|
|
469 | (1) |
|
Manually implementing an enumerator |
|
|
470 | (5) |
|
Implementing the Enumerable interface |
|
|
475 | (2) |
|
Implementing an enumerator by using an iterator |
|
|
477 | (1) |
|
|
478 | (1) |
|
Defining an enumerator for the Tree<TItem> class by using an iterator |
|
|
479 | (3) |
|
|
482 | (1) |
|
|
482 | (1) |
|
Chapter 20 Decoupling application logic and handling events |
|
|
483 | (30) |
|
|
484 | (1) |
|
Examples of delegates in the .NET class library |
|
|
485 | (2) |
|
The automated factory scenario |
|
|
487 | (3) |
|
Declaring and using delegates |
|
|
490 | (9) |
|
Lambda expressions and delegates |
|
|
499 | (1) |
|
Enabling notifications by using events |
|
|
500 | (1) |
|
|
501 | (1) |
|
|
502 | (1) |
|
Unsubscribing from an event |
|
|
502 | (1) |
|
|
502 | (1) |
|
Understanding user-interface events |
|
|
503 | (1) |
|
|
504 | (6) |
|
|
510 | (1) |
|
|
511 | (2) |
|
Chapter 21 Querying in-memory data by using query expressions |
|
|
513 | (24) |
|
|
513 | (1) |
|
Using LINQ in a C# application |
|
|
514 | (2) |
|
|
516 | (2) |
|
|
518 | (1) |
|
Ordering, grouping, and aggregating data |
|
|
519 | (2) |
|
|
521 | (1) |
|
|
522 | (3) |
|
Querying data in Tree<TItem> objects |
|
|
525 | (5) |
|
LINQ and deferred evaluation |
|
|
530 | (4) |
|
|
534 | (1) |
|
|
534 | (3) |
|
Chapter 22 Operator overloading |
|
|
537 | (22) |
|
|
537 | (1) |
|
|
538 | (1) |
|
|
539 | (1) |
|
Creating symmetric operators |
|
|
540 | (1) |
|
Understanding compound assignment evaluation |
|
|
541 | (1) |
|
Declaring increment and decrement operators |
|
|
542 | (1) |
|
Comparing operators in structures and classes |
|
|
542 | (1) |
|
|
543 | (1) |
|
|
544 | (3) |
|
Overriding the equality operators |
|
|
547 | (3) |
|
Understanding conversion operators |
|
|
550 | (1) |
|
Providing built-in conversions |
|
|
551 | (1) |
|
Implementing user-defined conversion operators |
|
|
552 | (1) |
|
Creating symmetric operators, revisited |
|
|
553 | (1) |
|
Writing conversion operators |
|
|
553 | (2) |
|
|
555 | (1) |
|
|
556 | (3) |
|
PART IV BUILDING UNIVERSAL WINDOWS PLATFORM APPLICATIONS WITH C# |
|
|
|
Chapter 23 Improving throughput by using tasks |
|
|
559 | (40) |
|
Why perform multitasking by using parallel processing? |
|
|
559 | (1) |
|
The rise of the multicore processor |
|
|
560 | (1) |
|
Implementing multitasking by using Microsoft NET |
|
|
561 | (1) |
|
Tasks, threads, and the ThreadPool |
|
|
562 | (1) |
|
Creating, running, and controlling tasks |
|
|
563 | (3) |
|
Using the Task class to implement parallelism |
|
|
566 | (10) |
|
Abstracting tasks by using the Parallel class |
|
|
576 | (4) |
|
When not to use the Parallel class |
|
|
580 | (2) |
|
Canceling tasks and handling exceptions |
|
|
582 | (1) |
|
The mechanics of cooperative cancellation |
|
|
582 | (12) |
|
Handling task exceptions by using the AggregateException class |
|
|
594 | (2) |
|
Using continuations with canceled and faulted tasks |
|
|
596 | (1) |
|
|
596 | (1) |
|
|
597 | (2) |
|
Chapter 24 Improving response time by performing asynchronous operations |
|
|
599 | (42) |
|
Implementing asynchronous methods |
|
|
600 | (1) |
|
Defining asynchronous methods: the problem |
|
|
600 | (3) |
|
Defining asynchronous methods: the solution |
|
|
603 | (6) |
|
Defining asynchronous methods that return values |
|
|
609 | (1) |
|
Asynchronous method pitfalls |
|
|
610 | (1) |
|
Asynchronous methods and the Windows Runtime APIs |
|
|
611 | (2) |
|
Tasks, memory allocation, and efficiency |
|
|
613 | (3) |
|
Using PLINQ to parallelize declarative data access |
|
|
616 | (1) |
|
Using PLINQ to improve performance while iterating through a collection |
|
|
616 | (5) |
|
|
621 | (1) |
|
Synchronizing concurrent access to data |
|
|
621 | (4) |
|
|
625 | (1) |
|
Synchronization primitives for coordinating tasks |
|
|
625 | (2) |
|
Canceling synchronization |
|
|
627 | (1) |
|
The concurrent collection classes |
|
|
628 | (1) |
|
Using a concurrent collection and a lock to implement thread-safe data access |
|
|
629 | (10) |
|
|
639 | (1) |
|
|
639 | (2) |
|
Chapter 25 Implementing the user interface for a Universal Windows Platform app |
|
|
641 | (46) |
|
Features of a Universal Windows Platform app |
|
|
643 | (2) |
|
Using the Blank App template to build a Universal Windows Platform app |
|
|
645 | (4) |
|
Implementing a scalable user interface |
|
|
649 | (10) |
|
Implementing a tabular layout by using a Grid control |
|
|
659 | (8) |
|
Adapting the layout by using the Visual State Manager |
|
|
667 | (7) |
|
|
674 | (11) |
|
|
685 | (1) |
|
|
686 | (1) |
|
Chapter 26 Displaying and searching for data in a Universal Windows Platform app |
|
|
687 | (30) |
|
Implementing the Model-View-ViewModel pattern |
|
|
687 | (1) |
|
Displaying data by using data binding |
|
|
688 | (6) |
|
Modifying data by using data binding |
|
|
694 | (5) |
|
Using data binding with a ComboBox control |
|
|
699 | (3) |
|
|
702 | (4) |
|
Adding commands to a ViewModel |
|
|
706 | (9) |
|
|
715 | (1) |
|
|
716 | (1) |
|
Chapter 27 Accessing a remote database from a Universal Windows Platform app |
|
|
717 | (54) |
|
Retrieving data from a database |
|
|
717 | (11) |
|
|
728 | (7) |
|
Creating and using a REST web service |
|
|
735 | (17) |
|
Updating the UWP application to use the web service |
|
|
752 | (9) |
|
Searching for data in the Customers app |
|
|
761 | (6) |
|
Inserting, updating, and deleting data through a REST web service |
|
|
767 | (2) |
|
|
769 | (1) |
|
|
770 | (1) |
Index |
|
771 | |