Automating Null-Forgiving Operator Removal in C# Projects

 
 
  • Gérald Barré
Nullable Reference Types are a feature in C# that helps developers avoid null reference exceptions by providing compile-time checks for nullability. This feature was introduced in C# 8.0 and is designed to improve code quality and reduce runtime errors. When the compiler cannot handle all cases, so developers may need to use the null-forgiving operator ! to suppress warnings. This operator tells the… [read more]

Using Pattern Matching in C# for Performance Optimization

 
 
  • Gérald Barré
The C# compiler is smart enough to understand some constructs and generates optimized code. However, C# gives you multiple ways to express the same intent. The compiler is not always smart enough to optimize every single ways. In this post, I will show you how to use pattern matching syntax to improve the performance of your code in some cases. Here's an example that checks if a version number is one of… [read more]

Resize Ubuntu Partitions in Hyper-V to Use Full Disk

 
 
  • Gérald Barré
When creating a new virtual machine in Hyper-V with Ubuntu Server with all the default settings, the partition will have 60GB of space instead of 127GB (default size of the virtual hard disk). Maybe there is a setting somewhere that I missed during the setup, but I couldn't find it. The solution is to resize the partition after the installation. This can be done with the following commands: First, check… [read more]

Use C# 14 extensions to simplify enum Parsing

 
 
  • Gérald Barré
In .NET, many types provide a static Parse method to convert strings into their respective types. For example: int.Parse("123"); double.Parse("123.45"); DateTime.Parse("2023-01-01"); IPAddress.Parse("192.168.0.1"); However, enums require the use of the Enum.Parse method: Enum.Parse<MyEnum>("Value1"); // MyEnum.Parse("Value1"); // This doesn't work Wouldn't it be more intuitive if enums supported a Parse… [read more]

StringComparison.InvariantCulture is not always invariant

 
 
  • Gérald Barré
CultureInfo.InvariantCulture is not invariant for all operations. It is a special culture that is used for formatting and parsing operations that do not depend on any specific culture. For instance, this is well-suited to format values in order to persist them. However, it is not invariant for string comparisons. Comparing strings using InvariantCulture can lead to different results depending on the… [read more]

Remove empty folders using PowerShell

 
 
  • Gérald Barré
Here's a PowerShell script that removes empty folders recursively: $rootFolder = 'C:\Temp' Get-ChildItem $rootFolder -Recurse -Directory -Force | Sort-Object -Property FullName -Descending | Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } | Remove-Item The logic is following: Get all directories recursively, use -Force to get hidden folders Sort them in descending order… [read more]

How to use a natural sort in PowerShell

 
 
  • Gérald Barré
PowerShell doesn't provide a built-in way to use a natural sort. However, you can use the Sort-Object cmdlet with a custom script block to achieve a natural sort. In this post, I describe how you can use a natural sort in PowerShell. What is a natural sort? A natural sort is a sorting algorithm that orders strings in a way that is more human-friendly. For example, a natural sort of the following strings:… [read more]