The set accessor in property and indexer declarations uses the implicit parameter value. This parameter acts as an input for the method. The word value refers to the value that client code tries to assign to the property or indexer.
ms.custom: "updateeachrelease"
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
In the following example, TimePeriod2 has a property named Seconds that uses the value parameter to assign a new string to the backing field _seconds. From the point of view of client code, the operation is written as a simple assignment.
sample · csharpdocs/csharp/language-reference/keywords/snippets/PropertyAccessors.csGetSetExpressions
namespace keywords;
// <GetSetAccessors>
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;
}
}
}
// </GetSetAccessors>
// <GetSetExpressions>
class TimePeriod2
{
private double _seconds;
public double Seconds
{
get => _seconds;
set => _seconds = value;
}
}
// </GetSetExpressions>
// <AutoImplementedProperties>
class TimePeriod3
{
public double Hours { get; set; }
}
// </AutoImplementedProperties>
// <FieldBackedProperty>
class TimePeriod4
{
public double Hours {
get;
set => field = (value >= 0)
? value
: throw new ArgumentOutOfRangeException(nameof(value), "The value must not be negative");
}
}
// </FieldBackedProperty>For more information, see the Properties and Indexers articles.
C# language specification
For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.