Using type aliases to make code clearer with generics

 
 
  • Gérald Barré
When you use generic types in C#, it is sometimes hard to understand what the generic argument can represent. For example, what do the strings represent in Dictionary<string, string>? To make the code clearer, you can use type aliases. // ❌ What does the string means? _ = new Dictionary<string, string>(); // Create aliases using UserId = string; using ProjectId = string; // Use the type aliases // ✅ It's… [read more]

Creating ico files from multiple images in .NET

 
 
  • Gérald Barré
Ico files are old but still widely used. One specificity of the ico format is that it can contain multiple images of different sizes. The format is not very complicated but there are not a lot of free tools to create an ico file from a list of images. Let's create one in .NET! The specification of the ico format is available on Wikipedia. The following code is a simple implementation of the specification.… [read more]

Convert DateTime to user's time zone with Blazor in .NET 8

 
 
  • Gérald Barré
This post is an update of the original post to take advantage of new .NET 8 features. It is inspired by the following pull request in dotnet/aspire When you display DateTime data to a user, you may want to convert the value to the user's time zone. With server-side Blazor, the code is executed on the server, so DateTime.Now corresponds to the time zone of the server instead of the user's time zone. At the… [read more]

Generate OpenAPI specification at build time from the code in ASP.NET Core

 
 
  • Gérald Barré
The OpenAPI specification is a powerful tool to describe and document APIs. It is a standard that allows you to define the structure of your API, including the endpoints, the request and response models, and the security requirements. The OpenAPI specification is a JSON or YAML file that can be used to generate documentation, client libraries, and server stubs. Most .NET developers generate the… [read more]

Set a blank page for new tabs in Microsoft Edge

 
 
  • Gérald Barré
Microsoft Edge can be configured using the setting pages edge://settings. However, some settings are not available in the UI. For instance, you cannot replace the new tab page. You can only configure the content of the page... So, you have to stick with the Edge-like page with news and other stuff you don't care about. Hopefully, you can configure Microsoft Edge using policies. There are multiple ways to… [read more]

Optional parameters may appear in the middle of the parameter list

 
 
  • Gérald Barré
In .NET, optional parameters are not always the last parameters. While C# does not allow the declaration of optional parameters in the middle of the parameter list, it is possible to do so in IL or from other languages like VB.NET or F#. Also, the compiler may lower some features and create methods with optional parameters in the middle of the parameter list. Why does it matter? If you write a source… [read more]