Monday, May 21, 2018

Property definition and implementation

You can see properties as fields with an implementation; within the implementation context they behave as variables, but the real implementation (that can be a storage field, a get or set method, or a combination) is hidden from the implementation.
A property is defined with the "property" keyword.

Simple properties
The simplest property you can write is:
This is a property that on completely behaves like a field; the property is read / write and the value is stored in a (hidden - compiler generated) field.
Just like a field, the property can be initialized with a default value:
The read only variant:
This read only property without initialization can only be initialized from the constructor.

Properties with explicit storage
Instead of the compiler generated storage field, you can define your own storage field.
To add a specific storage field for the property add the read and write modifiers:
The property above reads and writes to the field propstorage, and because this field is initialized to 5, the property is also. To create a read only property you can skip the write part, to create a write only property just skip the read part:

Functional properties
A property can also be defined with a get and a set method, where the function property implementation is defined:

In this case the set method is executed when the code writes to the property and the get method is executed when the code reads from the property.
Of course you can create read only and write only properties this way too:
Or combine the field storage and get or set method :

Parameterized or indexed properties
A property can also have one or more indexes (and because these indexers can be of any type, I call them parameterized properties). The get and set methods of the property must be changed to reflect those parameters:

Visibility modifier
Because a read/write property exists of two separate actions, we need a modifier to give them different visibility. By example, if you want a property that can be publicly read, but has to have a private setter you can write:
The first property uses a compiler generated field that has private write access, the second uses an explicit defined field with private write access. The same is true for functional properties; the methods itself have the visibility of the section that they are defined in, but the property still has only one visibility as long as the correct visibility is not defined in the read or write part of the property definition.

Instance and static properties
Just as with methods and field,  a property is a static property when the "class" keyword is used and
an instance property when it is not used.

The functional modifiers
These modifiers specify functional behavior of the property:
lockedThe locked modifier changes the behavior of the property in such a way that it becomes thread-safe; only one thread at a time can call this property. If more threads want to call this property simultaneously, the threads have to wait for each other.
locked on <expression>The locked on modifier behaves the same as locked, but works on an expression. If more properties or methods use the same expression than only one thread can run one of these properties or methods at once.
notifyThe notify modifier will make sure that there is a notification event raised when this property has been written. The way this functions varies between the different platforms.
defaultOnly for parameterized properties: The default modifier makes this property the default property of the object, so you can call MyObject[...params...] instead of MyObject.Propname[...params...].
Only one property in a type can have this modifier.

The compiler-hint modifiers
These modifiers help the compiler to generate the correct code for this property:
implements Form of implements IInterfaceName.PropertyName:
This compiler hint implements IInterfaceName.PropertyName tells the compiler which interface property is being implemented here.

Form of implements IInterfaceName:
This compiler hint tells the compiler that the property points to a type that implements the complete interface.
LazyOnly for initialized properties; tell's the compiler to initialize the property on first access instead of during execution of the constructor. A lazy property that isn't been called won't be initialized.
unsafe.Net only.
Tells the compiler that this property is doing unsafe operations like pointer operations. This modifier makes sure that the heap is not being compressed or garbage collected during the execution of this property.

And that was all there is to know about the properties.
The Generics will be explained in the next post.

No comments:

Post a Comment