Skip to content
  1. We help small teams achieve big things.
  2. Our Technical Fellow just became a Microsoft MVP!
  3. We run the Azure Weekly newsletter.
  4. We just won 2 awards.
  5. We just published Programming C# 8.0 book.
  6. We run the Power BI Weekly newsletter.
  7. Our NDC London 2020 talk is now available online!
  8. We are school STEM ambassadors.
Carmel Eve By Carmel Eve Software Engineer I
How to fully initialize types in their constructor with C# nullable using the async factory pattern

Here at endjin, we've recently enabled the new nullable references features in all of our projects. This has led me to a realisation that I often don't think as much as I should about object initialisation patterns. A couple of weeks ago I ran into an issue where I was trying to initalise an object, but the initialisation involved asynchronous operations so couldn't be done in a constructor.

When I first created the class I did this:

Where the asynchronous inititialisation is done via a factory method. However, this means that a set function is required for the property, and also, when nullable references are enabled you get this warning:

Warning message saying "Non-nullable property 'AString' is uninitialized. Consider declaring property as nullable.

This is because the AString property is not initialised in the constructor and is therefore technically able to be null. It would be better to be able to make the property truly readonly, and to ensure that it is always initialised as non-null.

To do this we can use the async factory method pattern:

Here we have a constructor which takes a string, which we use in the initialiser to produce a fully populated object. Also, as the constructor is private we know that the objects can only be initalisated from inside the Thing class. And, as the constructor takes a non-nullable string, we will get a warning if we try and pass anything that could be null in.

For example, if the GetStringAsync method returned a nullable string:

Then we would see this warning from the compiler when we try to pass it into the consructor:

Warning message saying "possible null reference assignment"

This is a simple pattern but one that I think is incredibly useful in avoiding partial instantiation and null reference exceptions!

Carmel Eve

Software Engineer I

Carmel Eve

Carmel has recently graduated from our apprenticeship scheme.

Over the past four years she has been focused on delivering cloud-first solutions to a variety of problems. These have ranged from highly-performant serverless architectures, to web applications, to reporting and insight pipelines and data analytics engines. She has been involved in every aspect of the solutions built, from deployment, to data structures, to analysis, querying and UI, as well as non-functional concerns such as security and performance.

Throughout her apprenticeship, she has written many blogs, covering a huge range of topics. She has also given multiple talks focused on serverless architectures. The talks highlighted the benefits of a serverless approach, and delved into how to optimise the solutions in terms of performance and cost.

She is also passionate about diversity and inclusivity in tech. Last year, she became a STEM ambassador in her local community and is taking part in a local mentorship scheme. Through this work she hopes to be a part of positive change in the industry.

Carmel won "Apprentice Engineer of the Year" at the Computing Rising Star Awards 2019.