Roadmap › OOP Concepts in C# › Module: Classes and Objects
Lessons in this course (15)
2. Constructors: default, parameterised, static, private, chaining
Basic★ Must knowLesson 2 of 15
csharp
public class Logger
{
public static readonly DateTime LoadedAt;
static Logger() { LoadedAt = DateTime.UtcNow; } // static ctor: once per type, before first use
public string Name { get; }
public Logger() : this("default") { } // chaining to another ctor
public Logger(string name) { Name = name; }
}
public class Settings
{
private Settings() { } // private ctor: no outside 'new'
public static Settings Load() => new Settings(); // factory method / Singleton
}
public class Admin : User
{
public Admin(string email) : base(email) { } // call base constructor
}- If you write no constructor, C# provides a parameterless one. Writing any constructor removes it.
- Order: static fields → static ctor (once) → instance field initialisers → base ctor → derived ctor body.
- Copy constructor: a constructor taking another instance of the same type (records generate one).