Property
I am so much confusing about why shoul we use Property instead of public field?If i have a public field simply i can initialize any value then what is the need of using property(get;set;) to initialize.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit ChoudharyPosted Apr 20, 2010, 2:35 AM
in simple language..
if you like to make your private class attributes with type of ReadOnly / Write Only the we use ... Properties..
MATHEWS wrote exactly what properties are mend for but its quite complicated to newbies.
Please mark as answer if it helps.
Sam HobbsPosted Apr 20, 2010, 2:10 PM
Jaish MathewsPosted Apr 20, 2010, 2:25 AM
Mainly encapsulation and data security
encapsulation
You may not always using a field as like. You may do lot of process on it. In side property you can add logic to process[May be business logics] your private variable, then only pass it to the usres. These logics are hidden from users. This is really helpful once you have public fields and lot of users started using it. Later you need to change the logic of that public field. In this scenario, using property make sure that you have less dependecy to modify those fields which are already using by varuious users.
datasecurity
It's always a good practice that not give direct acces to any of your class levesl fileds. Only give indirect access to you fields so that crrok users won't get a chance to spoil your class members integrity. So class members will be protected in class with private accebility and any public values will be exposed through property, i.e. only getting the value of the field not able to interacting with real field.