Properties

The default access modifier is private
A property is similar to a field except that you do not have to use additional methods to manage your private fields.
What can a property do that a field can't
A property that has both a set and get component is considered read-write

  • they can validate data before it is actually changed

  • they can take action when data is changed

  • they can change the values of other variables

  • they can raise exceptions


Auto Implemented Properties

An auto implemented property is equivalent to a property value that is stored in a private field
This is also sometimes referred to as Automatic Properties

public string Property_Name { get; set; } 

replaces

private string m_sName; 

public string Property_Name
{
   get { return m_sName; }
   set { m_sName = value; }
}


You can also create a read-only auto-implemented property by inserting the private keyword.

public string Property_Name { get; private set; } 


You cannot use auto implemented in the following cases:
add extra code to the Get and Set procedure
you don't want public accessors for get and set
properties that are writeonly or readonly
you want the property to accept parameters


VB allows a property to accept parameters, C# does not.



© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

Top

PrevNext