static lambda functions are not static methods

 
 
  • Gérald Barré
Stating with C# 9.0, you can use the static modifier on lambda functions. For instance, you can write the following code: enumerable.Select(static x => x * 2); When you use the static modifier on a lambda function, the compilers ensures that there is no capture of variables from the enclosing scope. This allows the compiler to generate better code. Indeed, it can cache the lambda function and reuse it… [read more]

How to iterate on a ConcurrentDictionary: foreach vs Keys/Values

 
 
  • Gérald Barré
There are multiple ways to iterate on a ConcurrentDictionary<TKey, TValue> in .NET. You can use the foreach loop, or the Keys and Values properties. Both methods provide different results, and you should choose the one that best fits your needs. Using the foreach loop The foreach loop lazily iterates over the key-value pairs in the ConcurrentDictionary. This means that the loop will only fetch the next… [read more]

How does the compiler infer the type of default?

 
 
  • Gérald Barré
The default keyword is a powerful tool in C#. For reference types, it returns null, and for value types (structs), it returns a zeroed-out value. Interestingly, default(T) and new T() can yield different results for structs (check out for more info). Initially, C# required default(T), but now you can simply use default when the type can be inferred by the compiler. But how does the compiler infer the… [read more]

Detect missing migrations in Entity Framework Core

 
 
  • Gérald Barré
Entity Framework Core allows to update the database schema using migrations. The migrations are created manually by running a CLI command. It's easy to forget to create a new migration when you change the model. To ensure the migrations are up-to-date, you can write a test that compares the current model with the snapshot model: [Fact] public async Task EnsureMigrationsAreUpToDate() { await using var… [read more]

Using Snapshot Testing to validate EF Core schema

 
 
  • Gérald Barré
When using Entity Framework Core, I prefer to view the generated schema as SQL. This allows me to easily validate what EF is doing behind the scenes and ensures that no attributes are missed, such as accidentally using nvarchar(max). My main objectives are: Be notified when the schema changes Review the new schema while editing it and during code reviews The solution I use is use is to have a test that… [read more]

Migrate from MSTest to xUnit using a Roslyn analyzer

 
 
  • Gérald Barré
Both MSTest and xUnit are great test framework. If you are curious about, I've written many blog posts about them MSTest series If you want to migrate to xUnit from MSTest, I've written a Roslyn Analyzer. This analyzer reports all MSTest attributes and assertions in your code. It can also replace most of them with xUnit equivalent. Note that the goal of this analyzer is not to handle 100% of the cases,… [read more]

Using Roslyn to analyze and rewrite code in a solution

 
 
  • Gérald Barré
I've written a lot about Roslyn in the context of Roslyn Analyzers and Source Generators. You can also use Roslyn as a library to analyze and generate code. For instance, you can create a console application that loads a solution, find patterns, and rewrite code. While Roslyn Analyzers are tied to a single project, you can use Roslyn as a library to analyze a whole solution. Let's create the console… [read more]