Creating C# Analyzers - Is EASY

Creating C# analyzers with the help of Roslyn is very easy. At first it seems a huge learning curve needed because of Roslyn and the complexity of compilers, but in reality, it is much simpler than expected.

There is one more advantage of creating analyzers. It give a perfect environment to practice Test Driven Development (TDD).

Before beginning we need to make sure, that the analyzer extensions are installed to Visual Studio, otherwise we will not have templates and Syntax Visualizer window. To install the SDK simply enable .NET Compiler Platform SDK feature in Visual Studio.

In this post I will show how to create an analyzer, which checks if an if statement or a conditional operator uses equality comparison operator to compare an operand with null. If that is the case, the analyzer will suggest a to replace the comparison with is pattern matching with nulls, which syntax is available with the latest C# version.

Find out more


ValueTask<T> versus Task<T>

Introduction

ValueTask has been recently introduced to C# to comprehend use-cases where allocation of a Task would be a problem. When it comes to deciding whether to use Task or ValueTask it is always suggested to measure the given application code path.

When a Task is awaited a state machine is being generated by the compiler. This state machine (a struct) executes through the asynchronous states of the method, also it preserves the internal state of the method. When we hit a state where we need to await another task, this struct will become boxed, so the state can be preserved between different threads that might execute different states of the method. When we execute synchronously though, no boxing would be required. However, the Task we return still needs to be allocated. The class library tries its best to use cached tasks, still we might hit a case where a new Task is being allocated. To avoid this allocation we can use ValueTask. Here are some remarks of the ValueTask documentation:

A method may return an instance of this value type when it's likely that the result of its operation will be available synchronously, and when it's expected to be invoked so frequently that the cost of allocating a new Task for each call will be prohibitive.

Find out more


Struct Serialization with Spans

In one of the books, Pro .Net Performance, an some examples are mentioned for struct serialization on the chapter of 'Code Generation'.

Since the book was released the C# has evolved, and some of the constraints has been removed earlier existed. Also with C# 7.2 and above we have Span at the hands of the developers so performance improvements can be implemented.

In this post, I will take the 2 deserialization examples of the book and compare it with the latest constructs available with Span and unmanaged generics.

Serialization task

Find out more


ConcurrentDictionary over List 4

In the previous post I have shown how we can use a new List with a ConcurrentDictionary in a thread safe manner.In this post I will show to other methods, to achieve the same.

Immutable Structures

One way to make our intentions clearer is to use immutabe collections. One package is right away available: System.Collections.Immutable

Here is an implementation for the test class to be used. According to my testing the ImmutableArray seems to be the immutable collection, that performs the fastest in this use cases I test.

Find out more


No Internet on Azure VM

I have been using a Virtual Machine on Azure for development purposes for over a year now. I had used this machine several times this week, but from one day to another, "the internet disappeared" on it. Obviously, it was connected to the network, as I was able to log with an RDP session, and even pinging IP-s worked just fine, but no websites were loaded by any browser.

Did some searches on the issue, and found a couple of posts in the topic, but nothing helped.

Symptoms

  • Pages don't load in the web browers

  • Cannot connect to OneDrive

  • My test application could not connect to other Azure resources, like table store

  • RDP works

  • Ping works

  • nslookup fails

  • I did not change anything explicitly on the machine

  • I did not change anything explicitly on the Virtual Network, Network Interface or Network Security group resources

Find out more