Activator.CreateInstance in C# – A Quick Rundown
Tags: csharp, dotnet, beginners, programming
Published: 2024-03-07 17:00:00

The post [Activator.CreateInstance in C# – A Quick Rundown](https://www.devleader.ca/2024/02/28/activator-createinstance-in-c-a-quick-rundown/) appeared first on [Dev Leader](https://www.devleader.ca).

`Activator.CreateInstance` in C# is one of the main tools we have when it comes to reflection in C#. This article is a quick look at how we can leverage `Activator.CreateInstance` as a brief [primer on using reflection within C#](https://www.devleader.ca/2024/02/26/reflection-in-c-4-code-simple-but-powerful-code-examples).

---

## What Is Activator.CreateInstance?

Now, let’s explore `Activator.CreateInstance` and understand how it enables dynamic object creation. The `Activator` class is a part of the `System` namespace in C# and provides methods for creating instances of types. The `CreateInstance` method specifically allows us to [create an instance of a type](https://www.devleader.ca/2023/05/31/implicit-operators-in-c-and-how-to-create-a-multi-type/) without having to declare it explicitly in code.

### How To Use Activator.CreateInstance?

To use `Activator.CreateInstance`, we first need to specify the type we want to create an instance of. We can pass either the type name or a `Type` object representing the type we want to instantiate. Additionally, if the type requires constructor arguments, we can pass them as parameters to `CreateInstance`.

Let’s consider a simple example where we have a class named `Person` with properties like `Name` and `Age`. We can dynamically create an instance of this class using `Activator.CreateInstance` as follows:

```csharp
Type personType = typeof(Person);
object personInstance = Activator.CreateInstance(personType);
```

In the above code snippet, we obtain the `Type` object for the `Person` class using the `typeof` operator. Then, we pass this type to `Activator.CreateInstance`, which returns an object instance of the `Person` class.

### Handling Constructor Arguments with Activator.CreateInstance

Often, classes require constructor arguments for their instantiation. We can handle this scenario by passing the required arguments to `Activator.CreateInstance`. Let’s extend our previous example to include a parameterized constructor in the `Person` class:

```csharp
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

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

Now, when creating an instance of `Person` using `Activator.CreateInstance`, we need to supply the necessary constructor arguments:

```csharp
Type personType = typeof(Person);
object personInstance = Activator.CreateInstance(
    personType,
    "John Doe",
    30);
```

In this case, we provide the arguments `"John Doe"` and `30` to `CreateInstance`, which will be used by the constructor of the `Person` class to initialize the properties.

---

## Quick Recap on Activator.CreateInstance in C#

`Activator.CreateInstance` is a helpful tool that we have access to when it comes to reflection in C#. We looked at a quick set of simple code examples that show us how to create objects and pass parameters in the constructors.

Reflection in C# is a powerful tool — but it’s easy to abuse. `Activator.CreateInstance` opens up new possibilities for creating flexible and extensible code, allowing for dynamic object creation and manipulation. By leveraging reflection ***properly*** (i.e. without overdoing it at every possible opportunity) in your projects, you can build some really cool things. It allows you to create objects on the fly, based on runtime information, and opens up opportunities for more dynamic systems — like leveraging plugins!

If you found this useful and you’re looking for more learning opportunities, consider [subscribing to my free weekly software engineering newsletter](https://subscribe.devleader.ca) and check out my [free videos on YouTube](https://www.youtube.com/@devleader?sub_confirmation=1)!

---

## **Want More Dev Leader Content?**

* Follow along on this platform if you haven’t already!    
* Subscribe to my free weekly software engineering and dotnet-focused newsletter. I include exclusive articles and early access to videos:  
    [**SUBSCRIBE FOR FREE**](https://subscribe.devleader.ca/)    
* Looking for courses? Check out my offerings:  
    [**VIEW COURSES**](https://devleader.ca/courses)    
* E-Books & other resources:  
    [**VIEW RESOURCES**](https://products.devleader.ca/)    
* Watch hundreds of full-length videos on my YouTube channel:  
    [**VISIT CHANNEL**](https://youtube.com/@devleader?sub_confirmation=1)    
* Visit my website for hundreds of articles on various software engineering topics (including code snippets):  
    [**VISIT WEBSITE**](https://devleader.ca/)    
* Check out the repository with many code examples from my articles and videos on GitHub:  
    [**VIEW REPOSITORY**](https://github.com/ncosentino/DevLeader)