HOWTO · Csharp
How to Create New Instance From Type in C#
This tutorial will discuss the methods of creating a new instance of a given data type at runtime in C#.
On this page
When the type is known while writing the program, use the new operator. When a Type value is selected or discovered at runtime, Activator.CreateInstance can create an instance without hard-coding the concrete class at the call site. If construction needs particular arguments, find the matching constructor with Type.GetConstructor and invoke it. The choice matters because constructors initialize objects; bypassing that work leaves the object outside its usual initialization path.
Create an Instance When the Type Is Known at Compile Time
Use new when the class is part of the program’s design and its name is available to the compiler. For example, Person person = new Person("Ada"); makes the selected type and constructor arguments explicit. The compiler checks that the type can be constructed and that the arguments match a constructor. A parameterless constructor can be called as new Person() if the class defines one (or receives one implicitly); adding a constructor with parameters means a class no longer receives an implicit parameterless constructor.
This is usually the clearest choice for ordinary application code. A struct can also be constructed with new, for example new Point(0, 0) when its constructor accepts two integers. new invokes the selected constructor, so it is the right option when the class’s normal initialization should happen.
Create an Instance at Runtime with Activator.CreateInstance
Use Activator.CreateInstance when the program has a Type value but cannot name the concrete class at the construction site. The example below uses a Type value to make the runtime choice explicit. It compares direct construction, parameterless activation, and a constructor selected by its parameter types. Run this example:
using System;
using System.Reflection;
public class Person
{
public string Name { get; }
public Person()
{
Name = "Unknown";
}
public Person(string name)
{
Name = name;
}
}
public class Program
{
public static void Main()
{
Person knownAtCompileTime = new Person("Ada");
Type selectedType = typeof(Person);
object activated = Activator.CreateInstance(selectedType);
ConstructorInfo constructor = selectedType.GetConstructor(new[] { typeof(string) });
object invoked = constructor.Invoke(new object[] { "Grace" });
Person withArguments = (Person)invoked;
Console.WriteLine(knownAtCompileTime.Name);
Console.WriteLine(((Person)activated).Name);
Console.WriteLine(withArguments.Name);
}
}
The parameterless Activator.CreateInstance(Type) overload requires a public parameterless constructor; it does not choose an arbitrary constructor or infer arguments. The example works because its Person type defines a public parameterless constructor, which initializes Name to "Unknown" when activation creates the instance. The returned value has the static type object; cast it to the expected class before using class-specific members. In a real program, the Type can come from configuration or another runtime decision, but the code still needs to know what kind of object it expects to receive.
Pass Constructor Arguments with Type.GetConstructor
Use reflection when the desired constructor has arguments or when you need to choose among constructors by their parameter types. Type.GetConstructor(new[] { typeof(string) }) looks for a constructor whose parameter list contains one string. Its result is a ConstructorInfo, and Invoke(new object[] { "Grace" }) calls that constructor with the corresponding argument. The example above then casts the returned object to Person and reads the initialized Name.
The parameter-type list is part of the lookup: a constructor taking an int is different from one taking a string, even if both accept one argument. Reflection is useful when the type or signature is dynamic, but it moves some checks from compile time to runtime. Ensure the requested signature exists and that each argument has a compatible value; otherwise lookup or invocation cannot produce the intended instance. Prefer new when the type and signature are already known because the compiler can verify that call directly.
Create an Uninitialized Object Only for Specialized Serialization Scenarios
An uninitialized object is created without running its instance constructor. The previous API name FormatterServices.GetUninitializedObject describes this constructor-bypassing operation, but this article does not assume that API is available or supported by every C# runtime. Do not use constructor bypass as a shortcut for ordinary object creation or to work around a private constructor.
Constructor code may establish invariants, allocate resources, or initialize fields that later methods assume are valid. Skipping it can leave an object in a state its class was not designed to handle. Only specialized serialization or infrastructure code with a documented lifecycle should consider this pattern, and it must restore and validate the object’s required state before normal use. For typical application code, use new, Activator.CreateInstance, or a selected ConstructorInfo so normal initialization runs.
Choose the Right Instance-Creation Approach
Use new for a type known at compile time and a statically known constructor. Use Activator.CreateInstance when a runtime Type should be instantiated through its accessible parameterless constructor. Use Type.GetConstructor plus ConstructorInfo.Invoke when runtime construction needs a particular signature and arguments. In each reflective case, check the type and constructor assumptions at the boundary where runtime data enters the program, then cast to the type the rest of the code expects.
These approaches all run a constructor when they use a normal constructor path. An uninitialized instance is a separate, specialized technique that skips that path and therefore carries additional state-management responsibility. Keeping that distinction clear helps choose the simplest construction mechanism that fits the available type information.