Book Image

C# 7 and .NET Core 2.0 High Performance

By : Ovais Mehboob Ahmed Khan
Book Image

C# 7 and .NET Core 2.0 High Performance

By: Ovais Mehboob Ahmed Khan

Overview of this book

While writing an application, performance is paramount. Performance tuning for realworld applications often involves activities geared toward fnding bottlenecks; however, this cannot solve the dreaded problem of slower code. If you want to improve the speed of your code and optimize an application's performance, then this book is for you. C# 7 and .NET Core 2.0 High Performance begins with an introduction to the new features of what?explaining how they help in improving an application's performance. Learn to identify the bottlenecks in writing programs and highlight common performance pitfalls, and learn strategies to detect and resolve these issues early. You will explore multithreading and asynchronous programming with .NET Core and learn the importance and effcient use of data structures. This is followed with memory management techniques and design guidelines to increase an application’s performance. Gradually, the book will show you the importance of microservices architecture for building highly performant applications and implementing resiliency and security in .NET Core. After reading this book, you will learn how to structure and build scalable, optimized, and robust applications in C#7 and .NET.
Table of Contents (11 chapters)
Designing Guidelines for .NET Core Application Performance

Writing quality code

For every performance-efficient application, code quality plays an important role. As we already know, Visual Studio is the most popular Integrated Development Environment (IDE) for developing .NET applications, and since Roslyn (.NET Compiler SDK) exposes compiler platforms as APIs, many features have been introduced that do not only extend the capabilities of Visual Studio, but enhance the development experience.

Live Static Code analysis is one of the core features that can be used in Visual Studio in developing .NET applications which provides code analysis during development while writing code. As this feature uses the Roslyn APIs, many other third-party companies have also introduced sets of analyzers that can be used. We can also develop our own analyzer for a particular requirement, and it's not a very complicated procedure. Let's look at a quick introduction on how we can use Live Static Code analysis in our .NET Core project and how it benefits the development experience by analyzing code and giving warnings, errors, and potential fixes for them.

We can add analyzer as a NuGet package. In, there are many analyzers available, and once we add any analyzer into our project, it adds a new Analyzer node into the Dependencies section of the project. We can then customize rules, suppress warnings or errors, and so on.

Let's add a new analyzer from Visual Studio in our .NET Core project. If you don't know which analyzer you want to add, you can just type analyzers in the NuGet Package manager window and it will list all the analyzers for you. We will just add the Microsoft.CodeQuality.Analyzers analyzer, which contains some decent rules:

Once the selected Analyzer is added, a new Analyzers node is added into our project:

In the preceding picture, we can see that three nodes have been added to the Analyzers node, and to see/manage the rules, we can expand the subnodes Microsoft.CodeQuality.Analyzers and Microsoft.CodeQuality.CSharp.Analyzers, which is shown as follows:

Moreover, we can also change the rule severity by right-clicking on the rule and selecting the severity, which is shown as follows:

In the preceding picture, rule CA1008 states that Enums should have a value of zero. Let's test this out and see how it works.

Create a simple Enum and specify the values, which are shown as follows:

public enum Status 
  Create =1, 
  Update =2, 
  Delete =3, 

You will notice as soon as you write this code, it will show the following error and it will provide potential fixes:

Finally, here is the fix we can apply, and the error will disappear:

You can also use one of the popular Visual Studio extensions known as Roslynator, which can be downloaded from the following link. It contains more than 190 analyzers and refactorings for C# based projects:

Live Static Code analysis is a great feature that helps developers to write quality code that conforms to the best guidelines and practices.