In C# 9 we saw how top-level statements can help us reduce ‘wasted’ vertical space… BUT… reducing ‘wasted’ horizontal space … Continue reading Use file-level namespaces in C# 10 to reduce vertical nesting
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…
Consider using named arguments in C# to increase readability of method calls
By using named arguments in C# we don’t have to match the ordering of parameter lists of called methods. Instead the … Continue reading Consider using named arguments in C# to increase readability of method calls
.NET 7 to include new ThrowIfNullOrEmpty ArgumentException guard clause
In .NET 6 Microsoft introduced the ArgumentException.ThrowIfNull guard clause and in .NET 7 (Preview 4) they have introduced ArgumentException.ThrowIfNullOrEmpty. Using … Continue reading .NET 7 to include new ThrowIfNullOrEmpty ArgumentException guard clause
System.Random is much faster in .NET 6 as Microsoft have changed underlying algorithm
In .NET 6 Microsoft have changed the algorithm used in System.Random and the performance improvements are crazy. Check out the … Continue reading System.Random is much faster in .NET 6 as Microsoft have changed underlying algorithm
Microsoft drops parameter null checking operator from C#11
Looks like Microsoft have pulled the null check !! operator from C#11. It had been included in C# 11 early … Continue reading Microsoft drops parameter null checking operator from C#11
Poll Results : Have you used Default Implementations in Interfaces in C# 8+ yet?
Default Implementations in Interfaces were introduced in C#8 and allow us add new members to a public interface without breaking … Continue reading Poll Results : Have you used Default Implementations in Interfaces in C# 8+ yet?
.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 … 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 … Continue reading Amazing performance improvement in .NET 6 when creating one SortedDictionary from another
ImmutableList performance improvements in .NET 6
Another small but welcome performance improvement in .NET 6 is with item retrieval times when using ImmutableList… It looks like … Continue reading ImmutableList performance improvements in .NET 6
How to set the Ratio column style in BenchmarkDotNet results
By default when you output the Ratio column in BenchmarkDotNet using the Baseline = true attribute it is output using … Continue reading How to set the Ratio column style in BenchmarkDotNet results
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 … 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 … Continue reading How to output the Ratio column when using BenchmarkDotNet
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