Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
{
public Person()
{
}
#region Property
private string _firstName = String.Empty;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
- Properties enable a class to expose a public way of getting and setting values.
- A get property accessor is used to return the property value, and a set accessor is used to assign a new value. These accessors can have different access levels. For more information, see Asymmetric Accessor Accessibility
- The value keyword is used to define the value being assigned by the set accessor.
- Properties that do not implement a set accessor are read only.
{
public Person()
{
}
#region Property
private string _firstName = String.Empty;
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}