Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 2.26 KB

File metadata and controls

40 lines (32 loc) · 2.26 KB
title set (C# Reference)
ms.date 03/10/2017
ms.prod .net
ms.technology
devlang-csharp
ms.topic article
f1_keywords
set
set_CSharpKeyword
helpviewer_keywords
set keyword [C#]
ms.assetid 30d7e4e5-cc2e-4635-a597-14a724879619
caps.latest.revision 14
author BillWagner
ms.author wiwagn

set (C# Reference)

The set keyword defines an accessor method in a property or indexer that assigns a value to the property or the indexer element. For more information and examples, see Properties, Auto-Implemented Properties, and Indexers.

The following example defines both a get and a set accessor for a property named Seconds. It uses a private field named _seconds to back the property value.

[!code-csharpset#1]

Often, the set accessor consists of a single statement that returns a value, as it did in the previous example. Starting with C# 7, you can implement the set accessor as an expression-bodied member. The following example implements both the get and the set accessors as expression-bodied members.

[!code-csharpset#3]

For simple cases in which a property's get and set accessors perform no other operation than setting or retrieving a value in a private backing field, you can take advantage of the C# compiler's support for auto-implemented properties. The following example implements Hours as an auto-implemented property.

[!code-csharpset#2]

C# Language Specification

[!INCLUDECSharplangspec]

See Also

C# Reference
C# Programming Guide
C# Keywords
Properties