| title | Properties (C# Programming Guide) | ||
|---|---|---|---|
| ms.date | 03/10/2017 | ||
| ms.prod | .net | ||
| ms.technology |
|
||
| ms.topic | article | ||
| f1_keywords |
|
||
| helpviewer_keywords |
|
||
| ms.assetid | e295a8a2-b357-4ee7-a12e-385a44146fa8 | ||
| caps.latest.revision | 38 | ||
| author | BillWagner | ||
| ms.author | wiwagn |
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
-
Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
-
A get property accessor is used to return the property value, and a set property accessor is used to assign a new value. These accessors can have different access levels. For more information, see Restricting Accessor Accessibility.
-
The value keyword is used to define the value being assigned by the
setaccessor. -
Properties can be read-write (they have both a
getand asetaccessor), read-only (they have agetaccessor but nosetaccessor), or write-only (they have asetaccessor, but nogetaccessor). Write-only properties are rare and are most commonly used to restrict access to sensitive data. -
Simple properties that require no custom accessor code can be implemented either as expression body definitions or as auto-implemented properties.
One basic pattern for implementing a property involves using a private backing field for setting and retrieving the property value. The get accessor returns the value of the private field, and the set accessor may perform some data validation before assigning a value to the private field. Both accessors may also perform some conversion or computation on the data before it is stored or returned.
The following example illustrates this pattern. In this example, the TimePeriod class represents an interval of time. Internally, the class stores the time interval in seconds in a private field named seconds. A read-write property named Hours allows the customer to specify the time interval in hours. Both the get and the set accessors perform the necessary conversion between hours and seconds. In addition, the set accessor validates the data and throws an xref:System.ArgumentOutOfRangeException if the number of hours is invalid.
[!code-csharpProperties#1]
Property accessors often consist of single-line statements that just assign or return the result of an expression. You can implement these properties as expression-bodied members. Expression body definitions consist of the => symbol followed by the expression to assign to or retrieve from the property.
Starting with C# 6, read-only properties can implement the get accessor as an expression-bodied member. In this case, neither the get accessor keyword nor the return keyword is used. The following example implements the read-only Name property as an expression-bodied member.
[!code-csharpProperties#2]
Starting with C# 7, both the get and the set accessor can be implemented as expression-bodied members. In this case, the get and set keywords must be present. The following example illustrates the use of expression body definitions for both accessors. Note that the return keyword is not used with the get accessor.
[!code-csharpProperties#3]
In some cases, property get and set accessors just assign a value to or retrieve a value from a backing field without including any additional logic. By using auto-implemented properties, you can simplify your code while having the C# compiler transparently provide the backing field for you.
If a property has both a get and a set accessor, both must be auto-implemented. You define an auto-implemented property by using the get and set keywords without providing any implementation. The following example repeats the previous one, except that Name and Price are auto-implemented properties. Note that the example also removes the parameterized constructor, so that SaleItem objects are now initialized with a call to the default constructor and an object initializer.
[!code-csharpProperties#4]
[!INCLUDECSharplangspec]
C# Programming Guide
Using Properties
Indexers
get keyword
set keyword