If you’ve created long strings using string interpolation ($) and hit enter to prevent horizontal scroll Visual Studio will + two interpolated strings together. In .NET 5 and below this didn’t really matter as string concat via + and $ both delegated to String.Concat(). In NET 6 interpolation works different and is faster and more … 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 don’t know exactly how big the list will be, a somewhat accurate estimate can still likely improve speed. Note though the higher our estimate is above the final list size the more unnecessary memory will … 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 so your references are type safe… If you have string literals containing property, class and namespace names all around your code you have to maintain them manually. If you rename a class which is referenced … 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 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 a rough estimate of how long our final string will be setting the capacity to this estimate can improve performance of StringBuilder dramatically. StringBuilder performance in C# is improved when setting capacity because StringBuilder doesn’t … 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 .NET string concatenation approaches to see which is the best performing and which is the most memory efficient. Below are benchmarks taken using BenchmarkDotNet for a simple scenario of just adding 4 strings with spacers … 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 keyword), disjunctive patterns (OR keyword) and negative patterns (NOT keyword). Two examples are below. These are simple examples of course but basically you don’t have to repeat the variable name as before and the syntax … 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 provides a succinct way to access single elements or ranges in a sequence. The first example below shows the ‘index from the end’ operator ^, which gives us a shorthand way of getting items relative … 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 C# string compare methods flagged? Well these string comparisons are not safe in all cultures. The canonical example is the ‘Turkish i problem‘ which relates to how in Turkish the dot is present on the … 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 certain assignments its best not to use it in these cases. Example 1 below shows some usages of var in which the type is very clear from the right hand side. In these cases var … Continue reading Don’t use the C# var keyword everywhere in your code just to be consistent