.NET 6 performance improvements when comparing two arrays using SequenceEquals

There’s some pretty impressive performance improvements in .NET 6 when we are comparing arrays using the LINQ SequenceEqual method. The benchmarks below show that on my machine comparing two int arrays of 10K elements was 94 times faster in .NET 6 compared to .NET 5. We can see that there is also impressive improvements when … Continue reading .NET 6 performance improvements when comparing two arrays using SequenceEquals

Amazing performance improvement in .NET 6 when creating one SortedDictionary from another

The performance improvements when cloning one SortedDictionary from another in .NET 6 compared to .NET 5 are amazing. I’m not sure how commonly devs need to clone a SortedDictionary but on my machine .NET 6 can do this over 25 times faster than .NET 5 so this will definitely help some apps. Click on the image … Continue reading Amazing performance improvement in .NET 6 when creating one SortedDictionary from another

Using declarations in C#8+ can allow us to dispose of resources correctly without increasing nesting levels

Since C#8 we can use single line using declarations which will allow us to dispose of resources at the end of the enclosing scope. This helps us reduce nesting in our code BUT… … while the using {} approach allows us to explicitly tell the compiler when to dispose of a resource, the C#8+ using declaration … Continue reading Using declarations in C#8+ can allow us to dispose of resources correctly without increasing nesting levels

How to output the Ratio column when using BenchmarkDotNet

If you’re using BenchmarkDotNet and want to see how other methods compare against a baseline method, set the Baseline attribute on your baseline method. Doing this will result in the Ratio column being outputted as shown below… More info from the Benchmark and Job Baselines page on BenchmarkDotNet.org.

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 methods… cool right? … but I wouldn’t use this when omitting the type name would result in ambiguity or confusion for the next developer about what class a static method is in. The examples below … 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 up our code for example when we might be repeatability referencing generic types. They also help to remove ambiguity when we have conflicting type names in difference namespaces. In this case without aliases we would … 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 where the variables are highly related (eg.. x, y or startDate, endDate). This way the reader can know they belong together but I wouldn’t be bunching a load of variables up just because they are … 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 nameof() when we are trying to get the string representation of a particular enum value… Why? … Well we can see from the IL below that nameof is evaluated at compile time and basically hardcodes … Continue reading ToString() v nameof() performance comparison when getting a string value from a enum option