GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  
 C#  |  Question 436 of 436    Print  
Public Members
Why do we use properties rather than public members?


  
Total Answers and Comments: 4 Last Update: November 19, 2009     Asked by: prasad.kshirsagar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: oliv29
 

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.



Above answer was rated as good by the following members:
masmas, alexisk75, Wise Man
September 08, 2009 05:42:57   #1  
PresidencyPrakash Member Since: September 2009   Contribution: 1    

RE: Public Members
By this way we can do some validations to the value being assigned to the properties.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 2Overall Rating: -1    
September 08, 2009 17:44:29   #2  
oliv29 Member Since: September 2009   Contribution: 1    

RE: Public Members

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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
October 09, 2009 21:41:29   #3  
netdev Member Since: October 2009   Contribution: 1    

RE: Public Members
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


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
November 19, 2009 17:55:52   #4  
statuomania Member Since: November 2009   Contribution: 1    

RE: Public Members
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 :)

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape