A property is defined with the "property" keyword.
The simplest property you can write is:
Just like a field, the property can be initialized with a default value:
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:
A property can also be defined with a get and a set method, where the function property implementation is defined:
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:
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:
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:
|locked||The 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.|
|notify||The 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.|
|default||Only 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.
|Lazy||Only 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.|
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.