DEV Community

Cristian Sifuentes
Cristian Sifuentes

Posted on

Partial Constructors and Events in C# 14 — Structuring Your Code Like a Pro

PartialConstructorsAndEventsInCsharp14

Partial Constructors and Events in C# 14 — Structuring Your Code Like a Pro

C# 14 pushes partial type support further by allowing constructors and events to be declared as partial members. This change empowers modularity and source generator scenarios, enabling more powerful abstractions in complex systems and large codebases.

In this post, you’ll learn:

  • What new partial members are allowed in C# 14
  • How to define partial constructors and events
  • How to use partial types with primary constructors
  • Best practices and caveats to avoid runtime surprises

What’s New in C# 14?

Previously, partial members were limited to methods, properties, and indexers. C# 14 adds:

  • Partial constructors
  • Partial events

These new members allow for separation of responsibilities across files or teams — great for large projects, generated code, or layered architectures.


Partial Constructors

You can now split a class constructor across multiple files.

Syntax:

Defining Declaration:

public partial class User
{
    public partial User(string name);
}
Enter fullscreen mode Exit fullscreen mode

Implementing Declaration:

public partial class User
{
    public partial User(string name)
    {
        Name = name ?? throw new ArgumentNullException(nameof(name));
    }

    public string Name { get; }
}
Enter fullscreen mode Exit fullscreen mode

🔗 Primary Constructor Compatibility

Only one of the partial declarations can use primary constructor syntax:

public partial class User(string name); // ✅ Only in one file
Enter fullscreen mode Exit fullscreen mode

Only the implementing part can include base() or this() initializers:

public partial class Customer : User
{
    public partial Customer(string name) : base(name) { }
}
Enter fullscreen mode Exit fullscreen mode

Partial Events

Events can now be defined across partial type declarations.

✅ Syntax:

Defining Declaration (field-like):

public partial class Service
{
    public partial event EventHandler? OnChange;
}
Enter fullscreen mode Exit fullscreen mode

Implementing Declaration (accessors):

public partial class Service
{
    public partial event EventHandler? OnChange
    {
        add => Console.WriteLine("Subscribed");
        remove => Console.WriteLine("Unsubscribed");
    }
}
Enter fullscreen mode Exit fullscreen mode

Useful when abstracting or instrumenting event handling logic for observability, logging, or distributed messaging.


Best Practices

Practice Benefit
Use partial constructors with generators Cleanly inject logic from T4 or source-gen
Separate event logic into concerns Handle instrumentation separately
Always pair definitions + implementations Partial members must be complete
Avoid multiple primary constructors Only one is allowed per partial class

Common Pitfalls

  • ❌ Only one partial declaration may use primary constructor syntax.
  • ❌ Partial constructor initializer (: base() or : this()) must appear only in the implementing part.
  • ❌ Both partial constructor parts must use identical parameter signatures.

Why This Matters

This enhancement is especially impactful when working with:

  • Source generators – e.g. add DI setup, logging, or initialization
  • Unit test scaffolding – split constructor validation logic
  • Event-driven architectures – separate event subscription logic

Learn More


Final Thoughts

The expansion of partial members in C# 14 unlocks better layering, clean separation of logic, and deeper integration with tooling.

When used responsibly, partial constructors and events make large-scale systems more maintainable, testable, and evolvable.

The more modular your code, the more powerful your architecture.


Written by: [Cristian Sifuentes] – Clean Code Advocate | Distributed Architect | C# Craftsman

Have you tried partial constructors in your domain models? Let’s discuss!

Top comments (0)