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# 10 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.
What Will You Learn:
Analyze alternative solutions before implementation by comparing pros and cons
Make polymorphic code perform better
Know the side effects of bad/redundant comments
Understand the significance of the SOLID and DRY principles
Add features using wrappers
Redefine steps without altering the calling sequence of an algorithm
Use hooks in your application
Convert a complex system into a user-friendly system using facades
Run your application in .NET 6
Who Is This Book For:
Developers with a basic knowledge of C#.
Author(s): Vaskaran Sarcar
Edition: 2
Publisher: Apress
Year: 2023
Language: English
Commentary: true
Pages: 322
Part I: Fundamentals
Chapter 1: Flexible Code Using Polymorphism
Recap of Polymorphism
Initial Program
Demonstration 1
Output
Analysis
Better Program
Demonstration 2
Analysis
Useful Notes
Summary
Chapter 2: Abstract Class or Interface?
Recap of Abstract Classes and Interfaces
Initial Program
Demonstration 1
Better Program
Demonstration 2
Output
Analysis
Summary
Chapter 3: Wise Use of Code Comments
Recap of Code Comments
Initial Program
Demonstration 1
Output
Analysis
Better Program
Demonstration 2
Analysis
Use the Power of C#
Demonstration 3
Analysis
Summary
Part II: Important Principles
Chapter 4: Know SOLID Principles
Single Responsibility Principle
Initial Program
Better Program
Open/Closed Principle
Initial Program
Better Program
Liskov Substitution Principle
Initial Program
Better Program
Analysis
Interface Segregation Principle
Initial Program
Better Program
Dependency Inversion Principle
Initial Program
Better Program
Summary
Chapter 5: Use the DRY Principle
Reasons for DRY
Initial Program
Demonstration 1
Output
Analysis
Better Program
Demonstration 2
Output
Analysis
Further Improvement
Demonstration 3
Output
Demonstration 4
Output
Summary
Part III: Make Efficient Applications
Chapter 6: Separate Changeable Code Using Factories
The Problem Statement
Initial Program
Demonstration 1
Output
Analysis
Better Program
Demonstration 2
Output
Analysis
A New Requirement
Demonstration 3
Output
Analysis
Demonstration 4
Output
Summary
Chapter 7: Add Features Using Wrappers
The Problem Statement
Initial Program (Using Subclassing)
Demonstration 1
Better Program (Using Object Composition)
Class Diagram
Demonstration 2
Output
Analysis
Summary
Chapter 8: Make Efficient Templates Using Hooks
The Problem Statement
Initial Program
Class Diagram
Demonstration 1
Output
Analysis
Enhanced Requirement
Demonstration 2
Output
Summary
Chapter 9: Simplify Complex Systems Using Facades
The Problem Statement
Initial Program
Demonstration 1
Output
Analysis
Better Program
Class Diagram
Demonstration 2
Output
Analysis
Summary
Part IV: Handling Surprises in a Better Way
Chapter 10: Organizing Exceptions
Recap of Exceptions
Case 1: Handling Different Exceptions in the Same Way
Initial Program
Better Program
Case 2: Handling the Same Exception in Different Ways
Initial Program
Better Program
Summary
Chapter 11: Special Attention to the Null Values
Initial Program
Demonstration 1
Output
Analysis
Better Programs
Demonstration 2
Output
Analysis
Demonstration 3
Analysis
Summary
Part V: The Road Ahead
Chapter 12: Memory Management
Overview
Stack Memory vs. Heap Memory
Q&A Session
The Garbage Collector in Action
Different Phases of Garbage Collection
Different Cases of Invoking the Garbage Collector
Demonstration 1
Output
Analysis
Q&A Session
Disposing of an Object
Finalize vs. Dispose
Demonstration 2
Output
Analysis
Q&A Session
Summary
Chapter 13: Analyzing Memory Leaks
Managed vs. Unmanaged Memory Leaks
Memory Leak Analysis
Demonstration
Snapshots from Diagnostic Tools
Summary
Chapter 14: More Tips
Learn Design Patterns
Brief History of Design Patterns
The Good News
Q&A Session
Avoid Anti-patterns
Brief History of Anti-patterns
Examples of Anti-patterns
Q&A Session
Final Suggestions
Decide Between a Static Method and an Instance Method
Know the Common Terminology
Accept Failures!
Q&A Session
Summary
Appendix A: Winning Notes
Appendix B: Resources
Index