0
How is the variable private here since we used it in a public property using accessor?
using System; namespace Sololearn { class Program { class Person { public string Name{set; get;} } static void Main(string[] args) { Person p = new Person(); p.Name = "Rohan"; Console.Write(p.Name); } } } Anybody here can access the 'Name' variable, though not directly, but through the accessor then I see no difference here using an auto-implemented propery. It seems to me that creating a public field is same to this. Help me here.!
1 Réponse
+ 4
Creating a public member may seem similar, but using property setter allows you to add some validation code or some restriction on the new data value, for example.
Also by using property you can limit access to the member, whether it is read-only (only property getter), write-only (only property setter), or like the code above, read + write.
Hth, cmiiw