Skip to content

The article on Object and Collection Initializers should mention and explain benefits of init and required. #35014

@konrad-jamrozik

Description

@konrad-jamrozik

I believe the article on Object and Collection Initializers should mention the init accessor as well as required modifier.

Specifically, it should be explained how these keywords enable advanced control over initializations, similar to how this section on Object Initializers with collection read-only property initialization does.

In brief:

init:

  • like a ctor param, but also enabling usage of object initializers;
  • another take: like a set accessor, but only during construction/init time, thus preventing mutability post-creation.

requried:

  • like init, but also must be set in object initializer, thus preventing only partially constructed objects (and precludes usage in ctor unless [SetsRequiredMembers] is used).

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

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