If it is not string

Introduction

In this post I am looking into the internals of the is not pattern, which is introduced in C# 9. This pattern allows the code to be more expressive. Assume to have a method with a type of an object input parameter, which needs validation. The method may only work if the object is a 2 character long string value. For example, we would like to validate that the string is a two letter country code.

C# 9 allows is not patterns. Using this pattern, we can express the above example with the following code:

using System;
object o1 = "UK";
if (o1 is not string countryCode || countryCode.Length != 2)
    Console.WriteLine("Invalid country code");
else
    Console.WriteLine(countryCode);

Find out more


Measuring Finalized Objects

This a quick post on how to measure the number of finalized objects with PerfView and WinDBG. The reason to measured this is because many appliciations use thousands of objects with finalizers. As finalization may not been suppressed on these objects, it can cause a significant performance degradation, which does not show up on tail latency, but only on throughput tests. Focusing only on a single operation only, can easily hide such an issue.

Even today I seem to run into libraries / code paths that excessively use objects with finalizers. .NET runtime's garbage collector handles finalizable objects separately from other objects. Housekeeping for live finalizable objects requires more resouces compared to non-finalizable ojects. Cleaning up dead finalizable objects requires even more resouces, including to run the Finalizer method, which is eventually user code.

When finalizers are combined with the dispose pattern, one should call GC.SuppressFinalize to exempt the objects from finalization. Thus saving the resoucers otherwise required for the cleanup. Although allocating objects with finalizers are still slower to regular objects.

Measuring the number of finalized objects

Find out more


Using FakeHttpMessageHandler

Introduction

HTTP calls are one of the most popular ways to do client-to-service or service-to-service communication these days. Most C# developers use HttpClient type to make http calls. Using HttpClient correctly, requires a good understanding on its internals, but for testing, this knowledge is unavoidable.

In this post, I will look into how the Goldlight.HttpClientTestSupport library can be used to help writing unit tests against code with HttpClient.

Why Testing?

Find out more


init-only properties

Init-only properties are a new addition to C# 9. The purpose of this blog post is to look into how it is represented in IL, as well to see how it behaves from IL verification's point of view.

What is an init-only property?

Properties, readonly properties and properties with private setter have been around for a while in C#. One use-case was still missing: setting properties with object initializers that are readonly. Previously these properties had to be read-write or values had to be passed through constructors, which involved writing a good set of constructors. The following code snippet shows an example of an init-only property. State class has a bool 'On' property, which is an init-only property.

var data = new State { On = true };
Console.WriteLine(data.On);

public class State
{
  public bool On { get; init; }
}

Find out more


JsonMergePatch

JsonMergePatch library provides an implementation for json merge patch operations, detailed in RFC7396. The library uses C# source generators to generate the types required for serialization. The Http package provides extension methods for HTTP requests and responses, while the AspNetCore package provides an InputReader implementation.

Resources in REST are typically updated according to the CRUD operations: Create/Update, Read, Delete. JsonMergePatch library enables to use http PATCH method to create and update a resource. Using PATCH operation has several benefits over PUT and POST requests:

  • Patch can be used for both create and update operations.

  • Patch payloads for updates can be significantly smaller if majority of the members remain unchanged

  • Patch solves versioning issues: a new version (V2) of the resource may contain new members. With patch operation V1 clients remain compatible: if a new property is unknown for the client, it will not be sent; on the server side the operation still succeeds, while the new V2 properties are retained. Using PUT operation, the V2 property values would become discarded by a V1 client request.

Getting Started

Find out more