C# 8 introduced a novel method for performing null coalescing assignments, as illustrated below.
DataTable table = null;
var testTable = new DataTable();
table ??= testTable;
Before C# 8, the code for this operation appeared as follows.
DataTable table = null;
var testTable = new DataTable();
if (table == null)
{
testTable = table;
}
Benchmark Results
Let’s examine the performance of various approaches to coding null coalescing. Previously, the performance difference between these two methods for coding null coalescing was negligible. However, as evident from these results, in .NET 8, utilizing the traditional approach for null checking proves to be slightly more performant.
Pick up any books by David McCarter by going to Amazon.com: http://bit.ly/RockYourCodeBooks
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearlyIf you liked this article, please buy David a cup of Coffee by going here: https://www.buymeacoffee.com/dotnetdave
© The information in this article is copywritten and cannot be preproduced in any way without express permission from David McCarter.