Skip to content

Instantly share code, notes, and snippets.

@ladeak
ladeak / ActivityLogWriter
Last active March 14, 2021 19:15
OpenTelemetryActivityLogWriter
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ConsoleApp2
{
public class ActivityLogWriter : IActivityWriter
{
private readonly ILogger<ActivityLogWriter> _logger;

Null-coalescing Operator vs. Equals - Equals

I have been asked the following question over-and-over: which solution is the faster and better to use in the condition part of the if keyword, to branch based on a property which might be defined on a nullable reference type:

  • to use the null-coalescing operator (??)
  • or the equals operator (==)

To give some context let's assume that we branch our code based on a reference type's bool property:

public class SomeClass
@ladeak
ladeak / refreturns2.md
Last active April 20, 2019 12:09
RefReturns under windbg

Deep Dive in Ref Returns

In the previous post I have used Ref returns to return some data. I noticed that with slight changes we get totally different code generated by the JIT, which is can have a good or bad effect on our code.

In this post, I will dig deep (with WinDbg) in the JIT generated code. As forefront: I am using 64 bit machine, .net core 2.1 and RyuJIT.

I created a sample benchmark to showcase. I have a Point struct with 2 integer properties. I benchmark setting the values on the struct in 3 different ways, I show related IL and machine code impacting performance.

The benchmark

@ladeak
ladeak / refreturns.md
Created April 16, 2019 17:21
RefReturns Blog Post

Ref Returns

C# has recently introduced some new features (version <7.0) one of which it is ref returns. I will use this feature in this post to further improve the performance of the Parser created in my previous post.

To refresh the Parser class looked as follows:

public class Parser 
{
 public T Parse(ReadOnlySpan input, PropertySetter[] setters) where T : struct