Table of Contents....5
About the Author....12
About the Technical Reviewers....13
Introduction....14
Part I: Fundamentals....22
Chapter 1: Flexible Code Using Polymorphism....23
Recap of Polymorphism....23
Initial Program....24
Demonstration 1....24
Output....25
Analysis....25
Better Program....27
Demonstration 2....27
Analysis....29
Useful Notes....35
Summary....38
Chapter 2: Abstract Class or Interface?....40
Recap of Abstract Classes and Interfaces....40
Initial Program....44
Demonstration 1....44
Better Program....48
Demonstration 2....48
Output....55
Analysis....56
Summary....57
Chapter 3: Wise Use of Code Comments....58
Recap of Code Comments....58
Initial Program....60
Demonstration 1....60
Output....61
Analysis....61
Better Program....61
Demonstration 2....62
Analysis....62
Use the Power of C#....63
Demonstration 3....65
Analysis....65
Summary....66
Part II: Important Principles....69
Chapter 4: Know SOLID Principles....70
Single Responsibility Principle....71
Initial Program....73
Demonstration 1....73
Output....75
Analysis....75
Better Program....76
Demonstration 2....76
Output....79
Open/Closed Principle....80
Initial Program....81
Demonstration 3....83
Output....86
Analysis....87
Better Program....88
Demonstration 4....89
Output....93
Analysis....93
Liskov Substitution Principle....94
Initial Program....95
Demonstration 5....97
Output....99
Better Program....102
Demonstration 6....102
Output....105
Analysis....105
Interface Segregation Principle....106
Initial Program....107
Demonstration 7....108
Output....110
Analysis....110
Better Program....112
Demonstration 8....112
Output....114
Analysis....114
Dependency Inversion Principle....115
Initial Program....116
Demonstration 9....117
Output....118
Analysis....118
Better Program....119
Demonstration 10....120
Output....122
Analysis....122
Summary....123
Chapter 5: Use the DRY Principle....125
Reasons for DRY....125
Initial Program....128
Demonstration 1....128
Output....129
Analysis....130
Better Program....130
Demonstration 2....131
Output....133
Analysis....133
Further Improvement....134
Demonstration 3....134
Output....137
Demonstration 4....140
Output....144
Summary....145
Part III: Make Efficient Applications....147
Chapter 6: Separate Changeable Code Using Factories....148
The Problem Statement....149
Initial Program....149
Demonstration 1....150
Output....152
Analysis....152
Better Program....153
Demonstration 2....155
Output....158
Analysis....158
A New Requirement....158
Demonstration 3....159
Output....161
Analysis....161
Demonstration 4....162
Output....164
Summary....164
Chapter 7: Add Features Using Wrappers....166
The Problem Statement....167
Initial Program (Using Subclassing)....167
Demonstration 1....167
Better Program (Using Object Composition)....170
Class Diagram....175
Demonstration 2....175
Output....180
Analysis....181
Summary....182
Chapter 8: Make Efficient Templates Using Hooks....184
The Problem Statement....184
Initial Program....185
Class Diagram....188
Demonstration 1....188
Output....190
Analysis....191
Enhanced Requirement....192
Demonstration 2....195
Output....198
Summary....199
Chapter 9: Simplify Complex Systems Using Facades....200
The Problem Statement....201
Initial Program....202
Demonstration 1....204
Output....205
Analysis....206
Better Program....206
Class Diagram....207
Demonstration 2....207
Output....210
Analysis....211
Summary....212
Part IV: Handling Surprises in a Better Way....213
Chapter 10: Organizing Exceptions....214
Recap of Exceptions....215
Case 1: Handling Different Exceptions in the Same Way....216
Initial Program....218
Demonstration 1....219
Output....220
Analysis....220
Better Program....220
Demonstration 2....222
Output....225
Analysis....226
Case 2: Handling the Same Exception in Different Ways....226
Initial Program....226
Demonstration 3....226
Output....228
Analysis....228
Better Program....228
Demonstration 4....229
Output....230
Analysis....231
Summary....231
Chapter 11: Special Attention to the Null Values....232
Initial Program....232
Demonstration 1....233
Output....235
Analysis....236
Better Programs....237
Demonstration 2....238
Output....240
Analysis....240
Demonstration 3....241
Analysis....243
Summary....244
Part V: The Road Ahead....245
Chapter 12: Memory Management....246
Overview....246
Stack Memory vs. Heap Memory....248
Q&A Session....251
The Garbage Collector in Action....256
Different Phases of Garbage Collection....256
Different Cases of Invoking the Garbage Collector....257
Demonstration 1....259
Output....261
Analysis....263
Q&A Session....263
Disposing of an Object....264
Finalize vs. Dispose....265
Demonstration 2....269
Output....271
Analysis....272
Q&A Session....273
Summary....278
Chapter 13: Analyzing Memory Leaks....280
Managed vs. Unmanaged Memory Leaks....281
Memory Leak Analysis....282
Demonstration....284
Snapshots from Diagnostic Tools....288
Summary....290
Chapter 14: More Tips....291
Learn Design Patterns....291
Brief History of Design Patterns....292
The Good News....295
Q&A Session....295
Avoid Anti-patterns....296
Brief History of Anti-patterns....297
Examples of Anti-patterns....298
Q&A Session....300
Final Suggestions....303
Decide Between a Static Method and an Instance Method....303
Know the Common Terminology....306
Accept Failures!....308
Q&A Session....308
Summary....310
Appendix A: Winning Notes....311
A Personal Appeal to You....311
Appendix B: Resources....312
Index....314
Apply skills and approaches to your programming to build a real-world application in C# 11 using the latest editions of Visual Studio, C#, and Microsoft .NET.This revised edition is updated with C#11 and places more emphasis on the newly introduced top-level statements. Additionally, you will find useful techniques and an explanation of the differences between writing code in two different styles. It also covers the new templates introduced in .NET 6, along with usage of .NET 7 in Windows 10 to write code and generate output.Each chapter opens with an introduction and original application written in C# 11 so that you can jump right into coding. From there, you are guided through an expected output and taught best practices along the way. Author Vaskaran Sarcar emphasizes extending and maintaining the same program and he demonstrates examples for different scenarios to make your program more efficient and effective.
This book is divided into five parts. The first part starts with a detailed discussion of polymorphism. It then shows you how to make proper use of abstract classes and interfaces, and teaches you to discern which technique to use for a specific scenario. Discussions on code comments teach you how to use them effectively, and why you need to be careful with code comments.
In the second part you will learn six design principles, including SOLID and DRY principles. These are the foundation of well-known design patterns, and they establish practices for developing software with considerations for maintaining and extending as a project grows.
The third part walks you through methods to make efficient applications. You will learn the common use of factories to separate code from its opposite and the alternative of inheritance using object composition and wrappers. This part also demonstrates the use of template methods, hooks, and facades in programming.Hints show you how professional coders develop an enterprise application.
Better handling of exceptions and null values is another integral part of professional programming, which the fourth part explores in detail. This will help you become a more professional programmer.
In the final part of the book, you will learn about effective memory management techniques and the use and misuse of design patterns. This part also briefly discusses how to decide between a static method and an instance method and other techniques.
After reading this book, you will be able to implement best practices to make your programs more effective and reliable.
For Developers with a basic knowledge of C#.