set (C# Reference)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

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 and examples, see Properties, Auto-Implemented Properties, and Indexers.

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 C# Language Specification. The language specification is the definitive source for C# syntax and usage.

C# Reference
C# Programming Guide
C# Keywords
Properties

Show: