Sunday, August 04, 2024

C# Partial Properties C# 13

Partial properties

Partial properties, like partial methods, are a new feature added in C# 13. They support source generators and are used to separate the declaration of a property from its implementation code. Note that partial methods were introduced earlier and gained some traction in C# 9.

The following code snippet illustrates how partial properties can be used:

partial class MyClass
{
    string myField;
    public partial string MyProperty { get; set; }
    public partial string MyProperty
    { get => myField; set => myField = value; }
}

It should be noted that when you declare a partial property with accessors having semicolon bodies without any implementation code inside, it is assumed to be a defining declaration. By contrast, when a partial property has accessors that contain implementation code, it is considered to be an implementing declaration.

Partial properties are used to provide support for source generators. Their sole purpose is to isolate the declaration of a property from its implementation code.

No comments:

Thumbs Up to GitHub Copilot and JetBrains Resharper

Having used AI tool GitHub Copilot since 08/16/2023, I’ve realized that learning GitHub Copilot is like learning a new framework or library ...