Easier immutability with Init only properties in C# 9

C# 9 shipped with .NET 5 in November. One change included is init only properties which allow us to have immutable (non-changeable) classes without some of the downsides associated with pre C# 9 approaches. As can be seen below in C# 8 we’d typically have getters with no setters which gives us immutability…great.. but it … Continue reading Easier immutability with Init only properties in C# 9

Parsing a nested value from a JSON string in .NET Core 3 without needing a DTO

.NET doesn’t use Newtonsoft by default anymore (since .NET Core 3.0). A lot of the examples online show how to use the new System.Text.Json namespace to extract values from JSON strings by deserializing the strings into full POCO classes (DTOs/ViewModels). If you only want to extract the value of a particular property and don’t want the … Continue reading Parsing a nested value from a JSON string in .NET Core 3 without needing a DTO

Generating sequential GUIDs which sort correctly in SQL Server in .net

Using a non sequential GUID in either a clustered or non clustered index is not ideal from a performance point of view as non sequential GUIDs are not ever-increasing (like an identity int) so get inserted into the middle of the index rather than the end resulting in increased logical fragmentation and decreased performance. If … Continue reading Generating sequential GUIDs which sort correctly in SQL Server in .net

Converting numbers to strings without scientific notation in C#

C# will automatically convert numbers which are of type float, double or decimal and have a lot of precision (lots of numbers after the decimal point) to scientific notation. The means if you have a double which for example contains the value .00009 and attempt to convert it to a string C# will display it as 9E-05. Of course … Continue reading Converting numbers to strings without scientific notation in C#

Discouraging use of the var keyword and ternary if operator

I would always favour typing more code to make it more explicit, more readable and to ensure consistency in style throughout a software system. Minimising the bytes and lines needed to do something shouldn’t take preference over readability. My two pet hates in this regard are the var keyword and ternary (?) if operator. I know var … Continue reading Discouraging use of the var keyword and ternary if operator