Skip to content

Instantiation

Context: Instantiation creates an object (instance) of a class using the new keyword, which allocates memory and calls the constructor.

ClassName variableName = new ClassName([arguments]);
Person person1 = new Person("Alice");
var person2 = new Person("Bob"); // using var
Person person3 = new() { Name = "Charlie" }; // target-typed new (C# 9+)

If you don’t define any constructor, the compiler provides a parameterless constructor that initializes fields to default values.

class Simple
{
public int Number;
}
Simple obj = new Simple(); // Number = 0

Set public fields/properties at creation.

var car = new Car { Model = "Tesla", Year = 2025 };