Code It Any Way You Want!

Back when .NET was first released in 2002, generally, there was only one way to code things. But now that we have the .NET Framework (CLR), .NET Core, and .NET, there can be many ways to code the same thing. Because of this, I have written a lot of benchmark tests to see if one way over the other is more performant. The results in this article will focus on showing the different ways where there is very little difference in .NET 5. So, code it any way you want! Well, code it the way that your coding standards show.

Testing String for Null

Which way do you check a string for null? Well, there is very little difference between these ways of coding it.

if (testString == null)
//or
if (testString is null)
//or
if (string.IsNullOrEmpty(testString))

Just remember that IsNullOrEmpty() also checks for nothing in the string, so my recommendation is to use it.

Creating an Instance

There are multiple ways to use Activator.CreateInstance() (shown below). These two ways are very close to the same speed.

Activator.CreateInstance(typeof(PersonProper));
//or
Activator.CreateInstance<Person>();

Returning a New Object Using a Void or Function

Most of the code I write to create a new object and then return it from a method, I use a function. Other developers prefer using a void with an out parameter. I was wondering if there is a speed difference and there is none.

private Person CreatePerson(int count) {
    return RandomData.GeneratePerson < Person > (cityLength: count);
}
private void CreatePerson(int count, out Person person) {
    person = RandomData.GeneratePerson < Person > (cityLength: count);
}

My standard way of coding is to use a function. Generally, using an out parameter is more confusing for developers. The RandomData class that generates random data for use in unit testing and benchmarking is from this NuGet package https://www.nuget.org/packages/dotNetTips.Spargine.5.Tester

Null Coalescing Assignment

C# 8 introduced a new way of doing null coalescing assignments as shown below,

DataTable table = null;
var testTable = new DataTable();
table ??= testTable;

Prior to C# 8, this is the way it was coded.

DataTable table = null;
var testTable = new DataTable();
if (table == null)
{
	testTable = table;
}

Both are very close to the same speed. For readability, I would suggest using the new way in C# 8.

Passing in an Array as a Parameter in Methods

When I want to have a parameter that allows the caller to pass in 1 or more values as an array is it more performant to pass it in as a normal array or using the params keyword. Well, both are very close to the same speed. Below are examples.

private void SomeMethod(int count, string[] args)
//or
private void SomeMethod(int count, params string[] args)

I recommend using params since it’s easier to use when calling it.

GetValueOrDefault vs Coalesce for Nullable Int’s

If you want to return a default value with a nullable int, there are two ways to do this.

private readonly int? _nullableInt = 10;
var result = nullableInt ?? 0;
//or
var result = nullableInt.GetValueOrDefault(0);

Both are close to the same speed.

Summary

As I discover more code that there is very little difference in performance, I will post it on my new performance page on dotNetTips.com at https://dotnettips.wordpress.com/code-performance/. There is much more performance information on that page, or you can pick up a copy of Rock Your Code: Code & App Performance for Microsoft .NET book by going here: https://www.amazon.com/dp/1693614863. All profit from the book is donated to the Voice of Slum NGO in Delhi, India.

If you have any comments or suggestions, please comment below.