Answered Questions

  • Public Members

    Why do we use properties rather than public members?

    Star Read Best Answer

    Editorial / Best Answer


    • Member Since Oct-2009 | Oct 9th, 2009

    To the user of an object, property and public member may not make much difference with respect to syntax. However, property provides the ability to combine both field and method via the get and set accessors.

    MS Help for C# presents the best answer for using properties:

    Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.



    • Apr 1st, 2010

    With Properties, we can implement validation.For example, if we have customer age and we want to add validation on  the age, we can do it with properties.Another benefits of properties is that we can protect a field in a class by reading and writing it through the properties.


    • Mar 7th, 2010

    Private data fields of class should not be exposed by making them public. Instead they can made visible using public properties in more controlled manner. Also we can make properties read only or read/write.