Properties (C# Programming Guide) - Microsoft Docs
Properties (C# Programming Guide) - Microsoft Docs
In this article
Properties overview
Properties with backing fields
Expression body definitions
Auto-implemented properties
Related sections
C# Language Specification
See also
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 overview
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 set accessor.
Properties can be read-write (they have both a get and a set accessor), read-
only (they have a get accessor but no set accessor), or write-only (they have a
set accessor, but no get accessor). 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.
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 ArgumentOutOfRangeException if the number of hours is invalid.
C# Copy
using System;
class TimePeriod
{
private double seconds;
class Program
{
static void Main()
{
TimePeriod t = new TimePeriod();
// The property assignment causes the 'set' accessor to be called.
t.Hours = 24;
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.
C# Copy
using System;
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.
C# Copy
using System;
class Program
{
static void Main(string[] args)
{
var item = new SaleItem("Shoes", 19.95m);
Console.WriteLine($"{item.Name}: sells for {item.Price:C2}");
}
}
// The example displays output like the following:
// Shoes: sells for $19.95
Auto-implemented properties
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.
C# Copy
using System;
class Program
{
static void Main(string[] args)
{
var item = new SaleItem{ Name = "Shoes", Price = 19.95m };
Console.WriteLine($"{item.Name}: sells for {item.Price:C2}");
}
}
// The example displays output like the following:
// Shoes: sells for $19.95
Related sections
Using Properties
Interface Properties
Auto-Implemented Properties
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
C# Programming Guide
Using Properties
Indexers
get keyword
set keyword