Records in C#

C# 9.0 supports records: Classes that receive auto-generated functions that faciliate a value-like comparison behaviour. They are well-suited for immutability, i.e. to not change after creation.

The introductory page shows a couple of usage patterns:

Declaration via positional parameters:

public class Duck(int Age, string Name);

Construction just like regular classes:

var myPetDuck = new Duck(3, "Peter Pan");

Copy updates (“non-destructive mutation”):

var petersPetDuck = myPetDuck with { Name = "Linkerbell" };

This copy is a shallow-copy, i.e. reference values are copied and link to the same sub-object.

Still to check:

  • Pattern matching with records

Benefits of Structured Logging

StackOverflow notes this question about structured logging, also known as semantic logging: https://softwareengineering.stackexchange.com/questions/312197/benefits-of-structured-logging-vs-basic-logging

Structured Logging stores log entries not as simple strings, but as the placeholder strings along with the values to input there, for example:

log("User {username} reached request limit {limit} /s", "Grumpel", 200);

It names two advantages of structured logs:

  • Events are implicitly grouped. The format string operates as a group for the entries it produces.
  • Data values are kept as data. The limit above stays an integer, allowing to query the logs for all entries with e.g. limit > 150.

Git(Hub) default branch name is now ‘main’

So, a little late to the party, but today I found GitHub’s default branch name to be main instead of the previous master. The change happens to switch to a less hurtful language, as can be read e.g. on the IETF document Terminology, Power and Oppressive Language. GitHub themselves switches all their repositories. Time to follow up, so I’ll start by updating all my repositories as well.

Addendum: Well, things spread. Even Firefox renamed the browser’s master password to primary password. Nice to see.