Skip to main content
Top

2023 | Book

Simple and Efficient Programming with C#

Skills to Build Applications with Visual Studio and .NET

share
SHARE
insite
SEARCH

About this book

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.

What Will You LearnAnalyze alternative solutions before implementation by comparing pros and consMake polymorphic code perform betterKnow the side effects of bad/redundant commentsUnderstand the significance of the SOLID and DRY principlesAdd features using wrappersRedefine steps without altering the calling sequence of an algorithmUse hooks in your applicationConvert a complex system into a user-friendly system using facades Run your application in .NET 6

Who Is This Book ForDevelopers with a basic knowledge of C#.

Table of Contents

Frontmatter

Funda

Frontmatter
Chapter 1. Flexible Code Using Polymorphism
Abstract
Ask a developer the following question: “What are the fundamental characteristics of object-oriented programming (OOP)?” You will hear an immediate reply saying that classes (and objects), inheritance, abstraction, encapsulation, and polymorphism are the most important characteristics of OOP. In addition, when you analyze OOP-based enterprise code, you’ll find different forms of polymorphism. But the truth is that a novice programmer rarely uses the power of polymorphism. Why? It is said that object-oriented programmers pass through three important stages. In the first stage, they become familiar with non-object-oriented constructs. In this stage, they use decision statements, looping constructs, etc. In the second stage, they start creating classes and objects and use the inheritance mechanism. Finally, in the third stage, they use polymorphism to achieve late binding and make their programs flexible. But writing the polymorphic is not always easy. Honestly, it is a little bit tough compared to the other features of OOP. Using some simple but powerful code examples, this chapter will make this concept easy for you to understand.
Vaskaran Sarcar
Chapter 2. Abstract Class or Interface?
Abstract
There are many code segments in which you can use an abstract class instead of a C# interface and vice versa. If the code is small and is used to perform a simple task, you may not see the difference between these two techniques. On the contrary, when the code is big and extendable, the choice between them can play a vital role in performance and maintenance.
Vaskaran Sarcar
Chapter 3. Wise Use of Code Comments
Abstract
Comments help you understand other people’s codes. They can describe the logic behind the code. But expert programmers are particular about comments and do not like to see unnecessary comments. This chapter provides case studies to help you decide whether to put a comment in your application.
Vaskaran Sarcar

Important

Chapter 4. Know SOLID Principles
Abstract
C# is a powerful language. It supports object-oriented programming and has lots of features. I believe that compared to the old days, coding is easier with the support of these powerful features. But the hard truth is that simply using these features in an application does not guarantee that you have used them in the right way. In any given requirement, it is vital to identify the purpose of classes, objects, and how they communicate with each other. In addition, your application must be flexible and extendable to fulfill future enhancements. Now the question is, where are these guidelines? To answer this, you need to follow experts’ footprints. Robert C. Martin is a famous name in the programming world. He is an American software engineer, is a best-selling author, and is also known as Uncle Bob. He has promoted many principles, and a subset of them are as follows:
Vaskaran Sarcar
Chapter 5. Use the DRY Principle
Abstract
This chapter discusses the don’t repeat yourself (DRY) principle. It is another important principle that a professional coder follows when writing a piece of code in an application. The SRP and OCP principles that you learned about in Chapter 4 are also related to the DRY principle. Andy Hunt and Dave Thomas first wrote about this principle in their book The Pragmatic Programmer. The DRY principle is stated as follows:
Vaskaran Sarcar

Make Efficient

Chapter 6. Separate Changeable Code Using Factories
Abstract
A developer’s end goal is to make an application that meets the requirements of the customer. But the code must be easily extensible, maintainable, and stable enough to meet future needs too. It is tough to write the best version of a program on the very first attempt. You may need to analyze the code and refactor it several times. In this chapter, you will see such a scenario and learn how to use factories. To simplify the discussion, I start with a small program. We’ll keep analyzing the program and modifying it accordingly.
Vaskaran Sarcar
Chapter 7. Add Features Using Wrappers
Abstract
An alternative to inheritance is composition. It is quite common in programming and often gives you a better payoff. This chapter shows you a useful case study on this topic using some wrappers.
Vaskaran Sarcar
Chapter 8. Make Efficient Templates Using Hooks
Abstract
This chapter will show you two important techniques. First, you will learn to use a template method. Why is this important? It is important because template methods are one of the fundamental techniques for code reuse. Suppose you follow a multistep algorithm to achieve a task. Using a template method, you can redefine some of these steps (but not all the steps) of the algorithm without altering the calling sequence of these steps.
Vaskaran Sarcar
Chapter 9. Simplify Complex Systems Using Facades
Abstract
In Chapter 8, you saw how you can redefine some steps in a multistep algorithm to achieve a task. In this chapter, you will look at an application that also does a series of tasks. But instead of redefining these tasks, you will learn how to make a simplified interface to perform this task. Facades are useful in this context.
Vaskaran Sarcar

Handling Surprises in a Better Way

Frontmatter
Chapter 10. Organizing Exceptions
Abstract
Errors in a program can be classified into the following categories: compile-time errors and runtime errors. Compile-time errors are comparatively easy to fix because you know about them much earlier (i.e., at compile time). These may occur mainly due to syntax errors or typing mistakes. The compiler can point to them so that you can take corrective measures immediately. By contrast, runtime errors are dangerous because you encounter them when a program is executing. Programmatically, we call them exceptions. Since the program was compiled earlier, you expected it to run successfully. Why do exceptions occur? These may occur through some careless mistakes; maybe you have implemented the wrong logic, you have ignored some loopholes in the code paths of the program, etc. This type of error is tough to handle and is costly. Even after our best efforts, often these situations are unavoidable. It is also true that many of the failures are beyond the control of a programmer. So, handling these runtime errors is essential when you write an application. C#’s exception handling mechanism is important because it helps you deal with these typical runtime errors in advance.
Vaskaran Sarcar
Chapter 11. Special Attention to the Null Values
Abstract
In the previous chapter, I told you about runtime exceptions. These are dangerous. Most often, they come in the form of the NullReferenceException in C#. Similarly, Java has the NullPointerException. The exception names can be different, but at the core, they arise when you try to access a member of an object whose value is null. Michael Feathers wrote the following in the book Clean Code:
Vaskaran Sarcar

The Road Ahead

Frontmatter
Chapter 12. Memory Management
Abstract
Memory management is an important concern for a developer, and it is a very big topic. This chapter will touch on the important points in a simplified manner to help you understand memory management in programming.
Vaskaran Sarcar
Chapter 13. Analyzing Memory Leaks
Abstract
Before we discuss memory leaks, let’s analyze a simple case study. It will help you understand how memory leaks can hamper the smooth execution of an application over the long run. Suppose you have an online application where users need to fill in some data and then click the submit button. We can assume that during this process, you need to use some resources that consume some blocks of computer memory. Obviously, once the process is completed, developers release the allocated blocks of memory. Now assume that unfortunately the developers of the application mistakenly forgot to deallocate some blocks during the process of execution. Because of this misjudgment, let’s say that the application leaks 512 bytes per click. You probably won’t notice any performance degradation from some initial clicks. But what happens if thousands of online users use the application simultaneously? If 1,000,000 users click the submit button, the application will eventually cause 488 MB of memory loss. You cannot allow this for a long time, because if it goes like this, the application becomes slower and slower, and in the worst case, the application can crash. These are common symptoms caused by memory leaks.
Vaskaran Sarcar
Chapter 14. More Tips
Abstract
This is the last chapter of the book. Earlier I told you that sometimes it’s OK to bend some well-accepted rules for your application if it performs well, but when you keep coding and developing applications, you’ll find that the experts’ suggestions have great value. If you follow their advice, you’ll understand that a simple choice can have a big impact in the long run. This chapter discusses some of these topics in brief.
Vaskaran Sarcar
Backmatter
Metadata
Title
Simple and Efficient Programming with C#
Author
Vaskaran Sarcar
Copyright Year
2023
Publisher
Apress
Electronic ISBN
978-1-4842-8737-8
Print ISBN
978-1-4842-8736-1
DOI
https://doi.org/10.1007/978-1-4842-8737-8

Premium Partner