Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

[C#] Support for init properties on generated classes #1154

@enioluwas

Description

@enioluwas

C# 9 introduced init properties that allow using object initializers to set readonly properties. This is quite useful for enforcing the copy-on-write pattern for immutable objects:

class Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }

    public Person(Person copy)
    {
        this.FirstName = copy.FirstName;
        this.LasName = copy.LastName;
    }    
}

var johnDoe = new Person { FirstName = "John", LastName = "Doe" };
var janeDoe = new Person(johnDoe) { FirstName = "Jane" };

I see the Bond compiler CLI already has a --readonly-properties switch for making private set properties on generated classes, but that is too restrictive for this use case. Is it possible to add a new switch such as --init-properties to allow the above code sample on a Bond-generated class?

Related: #439

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions