C# – How to initialize an array

Array is a fixed size collection of variables representing the same type. Array can store both built-in primitive types (like integer) and custom objects. In this article we’ll present different ways of initializing such arrays. Array of integers Let’s consider an array of integers. Below are presented five different methods of creating 3 elements array, … Continue reading C# – How to initialize an array

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 developers do which has some performance benchmarking uses it. It’s really easy to get started… 1 – Install BenchmarkDotNet from NuGet2 – Create a class which will contain your methods3 – Use the [Benchmark] attribute … 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 so we don’t need a using statement for it in each file which needs it. Ideally we’d put these global usings in a separate file. This will reduce so called ‘vertical waste’ which is nice.  … Continue reading Use C# 10 Global Usings to make namespaces available to all files in a project

Use file-level namespaces in C# 10 to reduce vertical nesting

In C# 9 we saw how top-level statements can help us reduce ‘wasted’ vertical space… BUT… reducing ‘wasted’ horizontal space and in particular nesting levels is much more valuable IMHO and in C# 10 (released November 2021) we have file-level namespaces to help with this. File-level namespaces are definitely one of my favourite features from … Continue reading Use file-level namespaces in C# 10 to reduce vertical nesting

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 matching parameter for each argument can be specified by parameter name. I never use these to change the position of arguments passed in as I find that can confuse other developers but I often use … 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 these kind of guard clauses instead of if statement checks results in cleaner code. Here is a example (from Steven Giesel on LinkedIn) with the new guard clause… I really like it, hopefully Microsoft can … 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 Ratio columns on the BenchmarkDotNet output below… WOW!!! Click on the image for a larger view in a new tab. There’s so many performance improvements in .NET 6 but the Random class is used in … Continue reading System.Random is much faster in .NET 6 as Microsoft have changed underlying algorithm