Public Members

Why do we use properties rather than public members?

Questions by prasad.kshirsagar

Editorial / Best Answer

netdev  

  • Member Since Oct-2009 | Oct 10th, 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.

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_csref/html/f7f67b05-0983-4cdb-96af-1855d24c967c.htm

Showing Answers 1 - 27 of 27 Answers

oliv29

  • Sep 8th, 2009
 

Because we can restrict the access from exterior to member variables of a class, by providing for example only a getter for that field, in this way providing encapsulation, one of the core principle of OOP.

netdev

  • 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.

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_csref/html/f7f67b05-0983-4cdb-96af-1855d24c967c.htm

statuomania

  • Nov 19th, 2009
 

Technically, there is nothing that can be acheived using property get/set code that cannot be replicated by using public get/set methods instead. 

But accessing properties is so common and useful, that a special syntax is provided for doing it, and property syntax is treated in a special way by the compiler, making it more efficient.
A property in C# is also known as an "accessor" method, but again this denotes what it is used for, more than to isolate anything unique about this kind of method.
A property differs primarily only in that you can execute a different code path depending not on the method name, but how and where it appears:
int x = Foo.Bar - executes the Get code
Foo.Bar = x - executes the Set code
But as above, this is owing to the syntax and compiler, not to a logical difference in how properties work as opposed to public methods.
This is a really good question, by the way :)

  Was this answer useful?  Yes

Properties are prefered over public members for the following reasons:
1) Create Read only property or create write only property or read-write properties.
2) We can set validations for the data read or written, in case of properties.
3) Properties are strongly typed when accessed through the instance of the class. The same is for Public members but they take more overhead in terms of memory management.

solanki.hr

  • Dec 10th, 2009
 

First of all Public members are open and can be accessed from anywhere within the assembly or classses or refrenced assemblies. This way there will not be any track of its value, it can be changed from anywhere.

Properties in turns provides Getter and Setter by which you can make property as read Only, write only or read & write. inside getter and setter you can have control of that property you can validate against its value(changing values) .
where as over Public fields or variable you can not have that control.

Thanks,
Solanki

  Was this answer useful?  Yes

SeeSharp

  • 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.

  Was this answer useful?  Yes

azita2005

  • 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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions