Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Should we use properties if we are not doing any data validation
Posted

1 solution

Yes, you should.

Properties are not related to validation. This is just the abstraction feature which allows you to present the syntax like in setting or reading a value to/from a field and hide side effects behind such assignment ("=") syntax. Typically, a property is backed by some underlying fields which is supposed (in a good programming style) to be private, but this is not required. However it's good to have one or another kind of assignment semantic for a property, otherwise some other features should be used.

Usually, using non-private fields is considered to be a bad style of programming. If you have some public member with assignment semantic, it should be a property. It allows for better supportability of projects: even if assignment or reading a value of some field does not require any side effect, you may need to implement it later. In this case, you can use auto-defined properties. If later you want to add some side effect, you leave the existing property member, only add a getter or a setter. This way, you won't need to change any code using your property, in contrast to the situation of you had a field and later decide to promote it to a method or two method to assign and read some value.

Please see: http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx[^].

—SA
 
Share this answer
 
Comments
mahakaal 15-Jun-13 2:05am    
private string name;
public string name
{
get { return name; }
set { name= value; }
}


//what is benefit of get set, if i do the same thing in this way

private string name:
public string name ()
{ return name;}
public name (aString)
{ name = aString;}
Sergey Alexandrovich Kryukov 15-Jun-13 22:40pm    
I already answered in detail. Which part of my explanation you did not understand? And now, do you see any benefit of the first form by yourself? Anyway, you sample is wrong and cannot compile. You cannot use two different members under the same name "name".

Besides, you should use auto-implemented form:

public string Mame { get; set; }

—SA

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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