Information Technology Reference
In-Depth Information
set ;
}
}
Yo u ' l l n o t i c e t h a t t h e l a s t e x a m p l e s u s e t h e C # 3 . 0 i m p l i c i t p r o p e r t y s y n -
tax. Creating a property to wrap a backing store is a common pattern.
Often, you won't need validation logic in the property getters or setters.
The language supports the simplified implicit property syntax to mini-
mize typing needed to expose a simple field as a property. The compiler
creates a private member field (typically called a backing store) for you
and implements the obvious logic for both the get and set accessors.
Yo u c a n e x t e n d p r o p e r t i e s t o b e a b s t r a c t a n d d e fi n e p r o p e r t i e s a s p a r t o f
an interface definition, using similar syntax to implicit properties. The
example below shows a property definition in a generic interface. Note
that while the syntax is consistent with implicit properties, the interface
definition below does not include any implementation. It defines a con-
tract that must be satisfied by any type that implements this interface.
public interface INameValuePair <T>
{
string Name
{
get ;
}
T Value
{
get ;
set ;
}
}
Properties are full-fledged, first-class language elements that are an exten-
sion of methods that access or modify internal data. Anything you can do
with member functions, you can do with properties.
The accessors for a property are two separate methods that get compiled
into your type. You can specify different accessibility modifiers to the get
and set accessors in a property in C#. This gives you even greater control
over the visibility of those data elements you expose as properties:
 
Search WWH ::




Custom Search