Creating objects without calling the constructor

Using GetUninitializedObject to create objects without calling the object constructor

Home DailyDrop

Daily Knowledge Drop

The RuntimeHelpers.GetUninitializedObject method can be used to create an instance of an object, without calling its constructor or property initializers.


Usage

Example class

Consider the following simple Person class:

public class Person
{
    public string Name { get; } = "(not set)";

    public int Age { get;  }

    public Person(int age)
    {
        Age = age;
    }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

The important things to note:

  • One constructor which explicitly sets both properties
  • One constructor which only explicitly sets the age
  • The Name property has an initializer, which sets the value if not explicitly set

Normal initialization

First let's look at "normal" object initializations - two instances of the Person class will be created, using the two different constructors:

var p1 = new Person("Dave", 46);
var p2 = new Person(47);

Console.WriteLine($"p1 has a name of '{p1.Name}' and an age of {p1.Age}");
Console.WriteLine($"p2 has a name of '{p2.Name}' and an age of {p2.Age}");

In the first instance, both properties are set, in the second instance only the age is (explicitly) set.

The output is then as follows:

p1 has a name of 'Dave' and an age of 46
p2 has a name of '(not set)' and an age of 47

As you might have expected, p1 outputs both values passed into the constructor, which p2 outputs the Age specified, and the default initialization value for the Name.


GetUninitializedObject

Next, we'll look at the RuntimeHelpers.GetUninitializedObject method, which is part of the System.Runtime.CompilerServices namespace.

using System.Runtime.CompilerServices;

var p1 = new Person("Dave", 46);
var p2 = new Person(47);
var p3 = (Person)RuntimeHelpers.GetUninitializedObject(typeof(Person));

Console.WriteLine($"p1 has a name of '{p1.Name}' and an age of {p1.Age}");
Console.WriteLine($"p2 has a name of '{p2.Name}' and an age of {p2.Age}");
Console.WriteLine($"p3 has a name of '{p3.Name}' and an age of {p3.Age}");

In the third instance, we use the GetUninitializedObject to get an uninitialized instance of Person.

The output is then as follows:

p1 has a name of 'Dave' and an age of 46
p2 has a name of '(not set)' and an age of 47
p3 has a name of '' and an age of 0

As the name of the method suggests, no initialization methods are called - neither the constructors nor the Name initializer.


Notes

A useful library to know about and leverage when the need arises. I wouldn't suggest using it to initialize objects, unless you know how the object will behave without having any constructor or initializers invoked. Doing so may cause instability in the usage of the instance.

One useful use case could be for auto generating documentation - if the documentation is to give examples of clean uninitialized entities, then this method could be used to get an object, which can then be serialized and output.


References

Create .NET Objects without Calling The Constructor

Daily Drop 96: 15-06-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net indexer