Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
C# 7 and .NET Core 2.0 High Performance

You're reading from   C# 7 and .NET Core 2.0 High Performance Build highly performant, multi-threaded, and concurrent applications using C# 7 and .NET Core 2.0

Arrow left icon
Product type Paperback
Published in Apr 2018
Publisher
ISBN-13 9781788470049
Length 300 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ovais Mehboob Ahmed Khan Ovais Mehboob Ahmed Khan
Author Profile Icon Ovais Mehboob Ahmed Khan
Ovais Mehboob Ahmed Khan
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. What's New in .NET Core 2 and C# 7? FREE CHAPTER 2. Understanding .NET Core Internals and Measuring Performance 3. Multithreading and Asynchronous Programming in .NET Core 4. Data Structures and Writing Optimized Code in C# 5. Designing Guidelines for .NET Core Application Performance 6. Memory Management Techniques in .NET Core 7. Securing and Implementing Resilience in .NET Core Applications 8. Microservices Architecture 9. Monitoring Application Performance Using Tools 10. Other Books You May Enjoy

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 NuGet.org, 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: https://marketplace.visualstudio.com/items?itemName=josefpihrt.Roslynator.

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

You have been reading a chapter from
C# 7 and .NET Core 2.0 High Performance
Published in: Apr 2018
Publisher:
ISBN-13: 9781788470049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime