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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit ChoudharyRank #145 · 15.8K reputationPosted Apr 19, 2010, 10:35 PM EDT
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.
Jaish MathewsRank #225 · 11K reputationPosted Apr 19, 2010, 10:25 PM EDT
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.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 20, 2010, 10:10 AM EDT