Information Technology Reference
In-Depth Information
A Property Example
The following code shows an example of the declaration of a class called C1 that contains a
property named MyValue .
￿
Notice that the property itself does not have any storage. Instead, the accessors deter-
mine what should be done with data sent in, and what data should be sent out. In this
case, the property uses a field called TheRealValue for storage.
The set accessor takes its input parameter, value , and assigns that value to field
TheRealValue .
￿
￿The get accessor just returns the value of field TheRealValue .
Figure 6-9 illustrates the code.
class C1
{
private int TheRealValue; // Field: memory allocated
public int MyValue // Property: no memory allocated
{
set
{
TheRealValue = value;
}
get
{
return TheRealValue;
}
}
}
Figure 6-9. Property accessors often use fields for storage
Search WWH ::




Custom Search