Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

set (C# Reference)

The set keyword defines an accessor method in a property or indexer that assigns the value of the property or the indexer element. For more information, see Properties (C# Programming Guide), Auto-Implemented Properties (C# Programming Guide), and Indexers (C# Programming Guide).

This is an example of a set accessor for a property named Seconds:

class TimePeriod
{
    private double _seconds;
    public double Seconds
    {
        get { return _seconds; }
        set { _seconds = value; }
    }


}

This is an example of a set accessor in an auto-implemented property:

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

For more information, see the following sections in the C# Language Specification:

  • 1.6.7.2 Properties

  • 10.7.2 Accessors

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft