More Partial Members in C# 13 — Properties & Indexers Join the Party

More Partial Members in C# 13 — Properties & Indexers Join the Party In C# 13, the partial keyword gets a meaningful upgrade: you can now declare partial properties and partial indexers, not just partial methods and types. This opens the door to cleaner separation of logic, tool-generated scaffolding, and clearer maintenance boundaries — especially in large-scale systems, source generators, and shared ownership codebases. Let’s break it down with examples, best practices, and limitations. Recap: What Is partial? The partial keyword allows a type or member to be defined in multiple files or split across declarations. Before C# 13, you could define: partial class, partial struct partial method But not properties or indexers — until now. What's New in C# 13? Now you can do this: public partial class C { // Declaration only (no body) public partial string Name { get; set; } } Then elsewhere (e.g., in another file or layer): public partial class C { private string _name; // Implementation public partial string Name { get => _name; set => _name = value; } } ✅ Clean separation ✅ Same type ✅ Compiler enforces signature match Real-World Example: Code Generation + Custom Logic File: Generated.cs public partial class User { // Generated contract public partial string Name { get; set; } } File: BusinessLogic.cs public partial class User { private string _name; public partial string Name { get => _name.ToUpperInvariant(); set => _name = value?.Trim(); } } This allows tooling or scaffolding to define contracts, while allowing developers to inject behavior. What About Indexers? Indexers can also be partial: public partial class DataStore { public partial int this[int index] { get; set; } } public partial class DataStore { private int[] _data = new int[10]; public partial int this[int index] { get => _data[index]; set => _data[index] = value; } } Same rules apply: the declaration must be fully matched in the implementation. Limitations Constraint Explanation Must match exactly Name, type, modifiers must match in both parts Only one can have a body Either the declaration or the implementation Cannot span multiple partial types Both must belong to the same partial class Auto-property implementation allowed Only on one side — others must have explicit bodies Why Use Partial Members? Use Case Benefit Codegen & custom separation Keep generated logic and handwritten logic separate Testing-specific implementations Implement testing hooks without polluting models Clean layering Domain vs infrastructure split with mutual contracts API Scaffolding tools Generate base structure and override behavior safely Learn More Partial Types and Methods Final Thoughts By enabling partial properties and indexers, C# 13 brings new power and flexibility to structured, layered, and tool-driven development. Whether you're separating concerns, injecting behavior, or integrating with source generators — this feature gives you one more tool to keep code clean, modular, and maintainable. Master this pattern and you'll unlock new ways to scale and organize enterprise-level C# systems. Written by: [Cristian Sifuentes] – .NET Tooling Specialist | Clean Code Architect | Partial Class Enthusiast Already using codegen tools or shared APIs? Tell us how partial properties will help you organize your logic!

May 7, 2025 - 22:49
 0
More Partial Members in C# 13 — Properties & Indexers Join the Party

MorePartialMembersInCSharp13

More Partial Members in C# 13 — Properties & Indexers Join the Party

In C# 13, the partial keyword gets a meaningful upgrade: you can now declare partial properties and partial indexers, not just partial methods and types.

This opens the door to cleaner separation of logic, tool-generated scaffolding, and clearer maintenance boundaries — especially in large-scale systems, source generators, and shared ownership codebases.

Let’s break it down with examples, best practices, and limitations.

Recap: What Is partial?

The partial keyword allows a type or member to be defined in multiple files or split across declarations.

Before C# 13, you could define:

  • partial class, partial struct
  • partial method

But not properties or indexers — until now.

What's New in C# 13?

Now you can do this:

public partial class C
{
    // Declaration only (no body)
    public partial string Name { get; set; }
}

Then elsewhere (e.g., in another file or layer):

public partial class C
{
    private string _name;

    // Implementation
    public partial string Name
    {
        get => _name;
        set => _name = value;
    }
}

Clean separation

Same type

Compiler enforces signature match

Real-World Example: Code Generation + Custom Logic

File: Generated.cs

public partial class User
{
    // Generated contract
    public partial string Name { get; set; }
}

File: BusinessLogic.cs

public partial class User
{
    private string _name;

    public partial string Name
    {
        get => _name.ToUpperInvariant();
        set => _name = value?.Trim();
    }
}

This allows tooling or scaffolding to define contracts, while allowing developers to inject behavior.

What About Indexers?

Indexers can also be partial:

public partial class DataStore
{
    public partial int this[int index] { get; set; }
}
public partial class DataStore
{
    private int[] _data = new int[10];

    public partial int this[int index]
    {
        get => _data[index];
        set => _data[index] = value;
    }
}

Same rules apply: the declaration must be fully matched in the implementation.

Limitations

Constraint Explanation
Must match exactly Name, type, modifiers must match in both parts
Only one can have a body Either the declaration or the implementation
Cannot span multiple partial types Both must belong to the same partial class
Auto-property implementation allowed Only on one side — others must have explicit bodies

Why Use Partial Members?

Use Case Benefit
Codegen & custom separation Keep generated logic and handwritten logic separate
Testing-specific implementations Implement testing hooks without polluting models
Clean layering Domain vs infrastructure split with mutual contracts
API Scaffolding tools Generate base structure and override behavior safely

Learn More

Final Thoughts

By enabling partial properties and indexers, C# 13 brings new power and flexibility to structured, layered, and tool-driven development. Whether you're separating concerns, injecting behavior, or integrating with source generators — this feature gives you one more tool to keep code clean, modular, and maintainable.

Master this pattern and you'll unlock new ways to scale and organize enterprise-level C# systems.

Written by: [Cristian Sifuentes] – .NET Tooling Specialist | Clean Code Architect | Partial Class Enthusiast

Already using codegen tools or shared APIs? Tell us how partial properties will help you organize your logic!