The const and readonly keywords in C# are used to declare variables that cannot be changed after they are initialized. … Continue reading Difference between const and readonly in C#
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…
Use Target Typed New Expressions to infer type on the right in C# 9 +
In C# 9 Target Typed New Expressions were introduced. They allow us to infer type on the right, whereas var … Continue reading Use Target Typed New Expressions to infer type on the right in C# 9 +
Use params keyword in C# to accept a variable number of arguments of the same type
Do you know about the PARAMS keyword in C# ? The params keyword allows us to create methods that accept … Continue reading Use params keyword in C# to accept a variable number of arguments of the same type
C# 11 list pattern examples
Beginning with C# 11, we can use list patterns to match elements of a list or array. Some examples of different types … Continue reading C# 11 list pattern examples
String.Equals with OrdinalIgnoreCase compared to ToLower / ToUpper for string insensitive comparisons in C#
In C#, when checking if strings are equal in a case insensitive way prefer string.Equals with OrdinalIgnoreCase over using ToLower() … Continue reading String.Equals with OrdinalIgnoreCase compared to ToLower / ToUpper for string insensitive comparisons in C#
String.Equals with OrdinalIgnoreCase faster in .NET 8 compared with .NET 7
There’s some awesome string comparison OrdinalIgnoreCase improvements coming in .NET 8, although I did see one regression too. Click on … Continue reading String.Equals with OrdinalIgnoreCase faster in .NET 8 compared with .NET 7
.NET 8 FrozenDictionary benchmarks
For those that don’t know Microsoft have introduced new FrozenDictionary and FrozenSet collections in .NET 8 (from Preview 1). These collections … Continue reading .NET 8 FrozenDictionary benchmarks
Consider C# 8 switch expressions to improve readability of existing switch statements
I generally don’t like too much ‘sugar’ being added to C# but Switch Expressions from C# 8 really can improve readability … Continue reading Consider C# 8 switch expressions to improve readability of existing switch statements
Use local functions in C# to encapsulate logic to a single calling method
💡 – Did you know we can use local functions in C# to scope logic only to their containing methods? … Continue reading Use local functions in C# to encapsulate logic to a single calling method
New Guard Clauses coming in .NET 8
New Guard Clauses coming in .NET 8 👇🏻 … .NET 6 and .NET 7 introduced a couple of Guard Clauses … Continue reading New Guard Clauses coming in .NET 8
Get method caller info in C# using info attributes
C# Tip 💡 – Did you know we can get info about the caller of a method using C# (5+) … Continue reading Get method caller info in C# using info attributes
.NET 7 increment operator ++ and +1 compiling differently
Since they are equivalent ++ and +1 should compile to the same code, right? As we can see below, this … Continue reading .NET 7 increment operator ++ and +1 compiling differently
Benchmark custom code workarounds against each new version of .NET as the framework gets faster all the time
Always revisit and benchmark your custom code workarounds with each version of .NET. The .NET framework and the JIT compiler gets faster … Continue reading Benchmark custom code workarounds against each new version of .NET as the framework gets faster all the time
String + versus StringBuilder benchmarks for 50K one char concatenations in .NET 7
I’m sure there’s still some junior .NET devs using + to concatenate large (usually an unknown) amounts of strings? Look … Continue reading String + versus StringBuilder benchmarks for 50K one char concatenations in .NET 7
File scoped types new in C# 11
File scoped types are new in C#11 and allow us to create types whose visibility is scoped to the source … Continue reading File scoped types new in C# 11
C# Explicit Operator example to map from one type to another
In .NET we have plenty of ways to map objects to one another. Below is an example, courtesy of ChatGPT … Continue reading C# Explicit Operator example to map from one type to another
.NET 6 v .NET 7 reflection performance benchmarks
.NET 7 (Preview 5+) has some awesome performance improvements when it comes to reflection functionality like invoking methods BUT there … Continue reading .NET 6 v .NET 7 reflection performance benchmarks
How to run benchmarks against multiple versions of .NET with BenchmarkDotNet
If you’re into this sort of stuff like me it can be fun to run benchmarks against bits of code … Continue reading How to run benchmarks against multiple versions of .NET with BenchmarkDotNet
Regular Expressions using RegexOptions.IgnoreCase slower than inline case insensitive pattern syntax in .NET 6
RegexOptions.IgnoreCase often offers better readability and expression of intent than inline pattern matching. We can see examples of the two … Continue reading Regular Expressions using RegexOptions.IgnoreCase slower than inline case insensitive pattern syntax in .NET 6
Three examples of Regular Expression performance improvements in .NET 7
Stephens Toubs performance articles on devblogs.microsoft.com are always something I look forward to. In May 2022 he wrote one covering … Continue reading Three examples of Regular Expression performance improvements in .NET 7
Favour Boolean assignment via expressions in C# rather than if/else blocks
Where applicable favour Boolean assignment via expressions rather than if/else blocks. The first example below is still so common but … Continue reading Favour Boolean assignment via expressions in C# rather than if/else blocks
Use the digit separator character in C# to improve readability of large numbers
Using the digit separator character _ in C# to break up your large numbers is just another little thing you … Continue reading Use the digit separator character in C# to improve readability of large numbers
Nested ternarys can be hard to read for other developers
Be careful with nested ternarys as they can impact code readability. For example I know what 👇🏻 … var comparison … Continue reading Nested ternarys can be hard to read for other developers
How to get started with BenchmarkDotNet using a simple example
BenchmarkDotNet has become the de facto standard for performance benchmarking on .NET. Nearly every video or blog post which Microsoft … Continue reading How to get started with BenchmarkDotNet using a simple example
Use C# 10 Global Usings to make namespaces available to all files in a project
Global Usings in C# 10 allow us to declare a namespace as being available to all files in a project … Continue reading Use C# 10 Global Usings to make namespaces available to all files in a project