Stable and Unstable Sorts in .NET

Sorting items of a collection is a common task that .NET developers perform. A collection is a data structure that stores multiple values, such as an array, a list, a dictionary, etc.

One common way to sort items of a collection in .NET 8 (and previous .NET versions) is by using LINQ’s OrderBy extension method. LINQ stands for Language Integrated Query, and it is a set of features that allow you to query and manipulate data in various ways. For example, you can use OrderBy to sort a list of names alphabetically:

// Create a list of names
List<string> names = new List<string>() { "Charlie", "Alice", "Bob" };

// Sort the list by name using OrderBy
var sortedNames = names.OrderBy(name => name);

Another way to sort items of a collection is by using Array.Sort method. This method works on arrays, which are fixed-size collections of values. For example, you can use Array.Sort to sort an array of numbers in ascending order:

Find out more

Building an IAQ device with Meadow

Indoor air quality (IAQ) is increasingly important for our health, productivity and well-being. The industrialized world suffers from air pollution, and buildings are no exceptions. A room's air might be filled with pollutants such as bacteria, carbon monoxide or carbon dioxide.

Meadow is a microcontroller that can run C# code. It is designed by Wilderness Labs. Meadow can be attached to a Project Lab, which is a functional IoT prototyping platform. It has numerous sensors built-in, buttons, a screen and a wide variety of connectors.

One particular sensor on the Project Lab board is the BME688 manufactured by Bosch. This sensor is interesting as besides the regular temperature, pressure, humidity, it can measure gas resistance. It is a metal oxide-based sensor that can detect gases by adsorption on its sensitive layer. It also comes with a software library, which can distinguish the different gases using AI. It can detect Volatile Organic Compounds (VOCs), Volatile Sulfur Compounds (VSCs), carbon monoxide and carbon dioxide.

The sensor also comes with an AI feature and a comprehensive SDK that enable developers to design and customize its behavior to their use-case. One such example I found online is the Cheese vs. Meat detector.

Find out more

Calculating U.S. Treasury Notes Pricing

A few years ago, I worked on a type that could process double values received as real-time market data updates and helped to convert them to be displayed for humans on a UI.

The type was used to process market data updates of U.S. Treasury Notes. The pricing of U.S. Treasury Notes is detailed on the cmegroup site. In this post I will focus on the pricing of bond and note products while keeping futures aside.

The prices of these products are displayed in fractions. The price of such a product may look as 99-032. This means it is 99 full points plus 3/32s of a point plus 2/8 of 1/32s of a point. The last digit of the price can take up values of 0 2, + and 6, where plus denotes 4/8 or 1/2.Hence 99-032 equals 99 + 3/32 + 2/256 = 99.1015625. Note, that the last digit is always a multiple of 1/4th of a 1/32nd. Another example may be 100-23+ which equals 100.734375. However, this post will focus on displaying such data, converting it from a double such as 100.734375 to 100-23+.

A few years back .NET was less performant. In this post, I will revisit the original implementation as well as review 3 implementations I would consider building today to take advantage of the performance benefits of the modern .NET. Even the original implementation is significantly faster when run on .NET 8 due to the number of optimizations that have been built into the BCL and the JIT.

Find out more

Using NDepend to improve my Application Code

NDepend is a .NET code quality tool that performs static code analysis. It can help to detect code smells and dependency issues. In this post I will take a quick look at using the Dependency Graph feature to clean up one of my open-source applications. NDepend has a desktop application (as well as Visual Studio extension) that can be used by developers in a tight development cycle to address dependency and code quality issues during development:

  • change code

  • build application

  • run analysis (or configure automatic analysis in NDepend for every build)

The tool also comes with a handy console application that can be leveraged for enforce quality rules on CI/CD agents. Later in this post I look at generating reports with the NDepend Console on build agents.

Dependency Graph

Find out more

Exploring Inline Arrays

Inline arrays have been introduced as new feature in C# 12. As the feature is preview at the time of writing, it may change before the .NET team releases the final version of C#.

A type attributed with inline array can be used to represent a fixed-size, contiguous sequence of primitives. The runtime and language provide type and overrun safe indexing operations to the underlying primitives.

At the time of writing one can define an inline array by applying the [InlineArray] attribute on a struct, that has a single field.

struct Octet
    private int _element0;

Find out more