If you’re using BenchmarkDotNet and want to see how other methods compare against a baseline method, set the Baseline attribute … Continue reading How to output the Ratio column when using BenchmarkDotNet
Category: C# Blog
C# blog posts covering topics such as usage tips, little known features, new features in upcoming versions of the language, performance tips including BenchmarkDotNet benchmarks and more…
Omit type name when calling static methods with the using static directive in C#
The using static directive was introduced in C# 6 and allows us to omit the type name when calling static … Continue reading Omit type name when calling static methods with the using static directive in C#
Using aliases in C# to provide shorthand references to types or namespaces
In C# we can use aliases to provide shorthand references to types or namespaces. Aliases can be useful to tidy … Continue reading Using aliases in C# to provide shorthand references to types or namespaces
Declaring and setting multiple variables of the same type in the same statement in C#
Does anyone actually use this variation of declaring variables? I rarely see it. I think its possibly useful in instances … Continue reading Declaring and setting multiple variables of the same type in the same statement in C#
ToString() v nameof() performance comparison when getting a string value from a enum option
The benchmarks (originally published on LinkedIn) below from Guilherme Ferreira show’s that there’s a big performance difference between using ToString() and … Continue reading ToString() v nameof() performance comparison when getting a string value from a enum option
Breaking up long interpolated strings can impact performance in .NET 6
If you’ve created long strings using string interpolation ($) and hit enter to prevent horizontal scroll Visual Studio will + … Continue reading Breaking up long interpolated strings can impact performance in .NET 6
Set List<> initial capacity to improve speed
FYI… setting capacity on a List<> when creating it can avoid a lot of expensive resizing operations. Even if we … Continue reading Set List<> initial capacity to improve speed
In C# use the nameof() expression so your property, class and namespace references are type safe
In C# if you’re referencing property, class or namespace names in your code use the nameof() expression rather than strings … Continue reading In C# use the nameof() expression so your property, class and namespace references are type safe
In C# use Math.Clamp to force a number between a particular inclusive range
In C# Math.Clamp is a helpful method if you need to force a value to stay within a particular inclusive … Continue reading In C# use Math.Clamp to force a number between a particular inclusive range
StringBuilder performance can be improved in C# by setting its capacity
When using StringBuilder in .NET most of times we likely just use the default constructor StringBuilder()… BUT if we have … Continue reading StringBuilder performance can be improved in C# by setting its capacity
.NET 6 string concatenation performance benchmarks
Since there’s been so many amazing performance improvements in .NET 6 I thought it would be fun to compare different … Continue reading .NET 6 string concatenation performance benchmarks
C#9 new AND, OR, NOT pattern matching keywords
C#9 (released Nov 2020) has some pattern matching enhancements. Added in C#9 (among other pattern changes) is conjunctive patterns (AND … Continue reading C#9 new AND, OR, NOT pattern matching keywords
Using C# indices and range syntax to access elements in a sequence
Microsofts Indices and Ranges page provides examples of the indices and range syntax available in C# (introduced in C#8). This syntax … Continue reading Using C# indices and range syntax to access elements in a sequence
Using ToLower() or ToUpper() in C# to compare strings is not safe in all cultures
If you’re using Resharper, Roslynator or similar you’ll likely see string comparisons using ToLower() or ToUpper() flagged. Why are these … Continue reading Using ToLower() or ToUpper() in C# to compare strings is not safe in all cultures
Don’t use the C# var keyword everywhere in your code just to be consistent
Don’t use the var keyword everywhere in your code base just to be consistent. If using var removes clarity for … Continue reading Don’t use the C# var keyword everywhere in your code just to be consistent
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 … 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 … 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 … 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 … 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 … Continue reading Discouraging use of the var keyword and ternary if operator