แก้ไข

แชร์ผ่าน


The get keyword

The get keyword defines an accessor method in a property or indexer that returns the property value or the indexer element. For more information, see Properties, [Automatically implemented Properties](../../programming-guide/classes-and-structs/automatically implemented-properties.md), and Indexers.

For simple cases in which a property's get and set accessors perform no other operation than setting or retrieving a value in a private backing field, you can take advantage of the C# compiler's support for automatically implemented properties. The following example implements Hours as an automatically implemented property.

class TimePeriod3
{
    public double Hours { get; set; }
}

Important

Automatically implemented properties aren't allowed for interface property declarations or the implementing declaration for a partial property. The compiler interprets syntax matching an automatically implemented property as the declaring declaration, not an implementing declaration.

Often, the get accessor consists of a single statement that returns a value, as it did in the previous example. You can implement the get accessor as an expression-bodied member. The following example implements both the get and the set accessor as expression-bodied members.

class TimePeriod2
{
    private double _seconds;

    public double Seconds
    {
        get => _seconds;
        set => _seconds = value;
    }
}

You might find that you need to implement one of the accessor bodies. You can use a field backed property to let the compiler generate one accessor while you write the other by hand. You use the field keyword, added as a preview feature in C# 13, to access the compiler synthesized backing field:

class TimePeriod4
{
    public double Hours {
        get;
        set => field = (value >= 0)
            ? value
            : throw new ArgumentOutOfRangeException(nameof(value), "The value must not be negative");
    }
}

Important

The field keyword is a preview feature in C# 13. You must be using .NET 9 and set your <LangVersion> element to preview in your project file in order to use the field contextual keyword.

You should be careful using the field keyword feature in a class that has a field named field. The new field keyword shadows a field named field in the scope of a property accessor. You can either change the name of the field variable, or use the @ token to reference the field identifier as @field. You can learn more by reading the feature specification for the field keyword.

The following example defines both a get and a set accessor for a property named Seconds. It uses a private field named _seconds to back the property value.

class TimePeriod
{
    private double _seconds;

    public double Seconds
    {
        get { return _seconds; }
        set
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), "The value of the time period must be non-negative.");
            }
            _seconds = value;
        }
    }
}

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See also