Automatically implemented properties

 

Introduction

Automatically implemented properties make property declaration more concise, where no other logic is required in property accessors. The compiler creates a private and anonymous storage field that can only be accessed by the property's get and set accessors.

 

 

Generating automatically implemented properties

In order to allow the declaration of properties using simplified syntax, you must first specify version 3.x of the language in the C# Developer "Général" parameter set (as shown below).

 

The "SDK Version" parameter set to version 3.x


This new feature of the C#3.0 language can subsequently be activated or deactivated using the "Authorize automatically implemented properties" parameter in the "Code generation" set.

 

The "Authorize automatically implemented properties" parameter

 

To obtain the property declaration with simplified syntax, you must not define any associated code notes (CsGetCode/CsSetCode), as if you do, a normal property will be generated.

 

The generator will automatically apply the "private" modifier (by default) to the get or set accessor according to which access mode has been selected, as shown in the table below.

 

Access mode / Modifiers

None

Read

Write

ReadWrite

get

Error

Warning

CsPropertyVisibilityGet or private

Warning or Error

set

Error

CsPropertyVisibilitySet or private

Warning

Warning or Error

 

Note:      You can change the default modifier of get and set accessors using the corresponding "CsPropertyVisibilityGet" and "CsPropertyVisibilitySet" tagged values.


 

 


UML Model

C# Generated

 

 

 

using System;

using System.Diagnostics;

 

namespace ExtensionMethods

{

    public class LightweightCustomer

    {

#region TotalPurchases

        public int TotalPurchases { get; set; }

#endregion

 

#region Name

        public string Name { get; private set; } // read-only

#endregion

 

#region CustomerID

        public int CustomerID { private get; set; } // write-only

#endregion

    }

}

 

Example of automatically implemented properties