Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am C++/VC++ developer but know .Net up to some extent.

In .Net 3.5 following syntax is permissible.

C#
public int PropertyName {get;set}


Not how this property conceptually differed from normal varialble.


Regards
Posted
Comments
VJ Reddy 12-Jun-12 4:07am    
Thank you for reviewing and accepting the solution :)

This has been answered so many times and Google has tons of very good explanations about it. Few links are:

http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html[^]

http://channel9.msdn.com/forums/Coffeehouse/253880-GetSet-vs-Public-Variables/[^]
 
Share this answer
 
Comments
VJ Reddy 12-Jun-12 3:55am    
Good references. 5!
Manas Bhardwaj 12-Jun-12 3:57am    
thx!
Prasad_Kulkarni 12-Jun-12 5:09am    
Good links Manas!
Manas Bhardwaj 12-Jun-12 5:24am    
Thanks!
Espen Harlinn 12-Jun-12 8:38am    
5'ed!
Properties have a lot of advantages over public variables, so much so that public variables are considered very bad practice.

The problem is that if you declare a public variable, you are letting an external class have access to your class internals at any time, in a unregulated way. This in turn means that when you come to modify or maintain a class, you have to consider all possible external effects on the public variables at every stage, or risk breaking external code with your changes.

Properties have these advantages:
1) You can have separate access levels for Getters and Setters - so external classes can only read the value, while derived classes can modify it.
2) You can include code in the setter which verifies that the data is valid before setting it - and either change it so it is, or throw an error - this allows the external class to spot a problem at the point when it occurs, rather than half an hour later when you try to use the bad value in your code.
3) Properties are handled very differently by the framework. This difference means that when you add (for example) a user control in the designer, only properties appear in the Property Pane for the instance - public variables don't.
4) Properties allow you to hide the implementation from the outside world: all it sees is an integer that tells it how many items you are holding - it doesn't have to know that you don't keep that value at any time, but that when he asks for it, you calculate the number. This lets you have a lot greater freedom to redesign the internals of your code without affecting outside applications.
5) It forces you to think about what your class needs to expose - because it is slightly more cumbersome to establish a property, you have to think "does he need this information?" - and if he doesn't, then it should be internal only.
 
Share this answer
 
Comments
Manas Bhardwaj 12-Jun-12 3:49am    
Well explained +5!
VJ Reddy 12-Jun-12 3:52am    
Nice explanation. 5!
[no name] 12-Jun-12 4:02am    
5!
Espen Harlinn 12-Jun-12 8:38am    
5'ed!
C#
public int PropertyName {get;set}

is an auto implemented property, which will be expanded by the compiler and the back end store will be created like
C#
//Some name will be given by the compiler for the back end store
//here I have used propertyName only for illustration
private int propertyName
public int PropertyName {
    get { return propertyName; }
    set { propertyName = value;}
}

So, an auto implemented property is a normal property for the executing code and its only a shortcut to the programmer when no other actions like validations are required to be performed within the getter and setter of the property.

Now, the difference between a variable and a property is that the value of the variable is directly set and retrieved, whereas in case of Property it is set in setter and retrieved in getter which permits to perform extra operations like validations.

The .NET Framework design guideline does not recommend the usage of public variable.

Further, in .NET data binding cannot be done with variable but the data binding can be done with property.
 
Share this answer
 
v3
Comments
Manas Bhardwaj 12-Jun-12 3:50am    
Well explained +5!
VJ Reddy 12-Jun-12 3:53am    
Thank you, Manas :)
Prasad_Kulkarni 12-Jun-12 5:09am    
To the point +5!
VJ Reddy 12-Jun-12 5:50am    
Thank you, Prasad :)
Espen Harlinn 12-Jun-12 8:38am    
5'ed!
In case of public get and set it wouldn't differ from a public variable.
However you can have properties that have public get and a private set..i.e you can vary the scope for modifying values or reading values using properties which is not possible with variables.
Hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900