Typically you want to encapsulate data by providing getters and setters for your internal state. Example:
public class Person {
private int age;
public int Age {
get { return age; }
set { age = value; }
}
}
void Main() {
Person alice = new Person();
alice.Age = 20;
}
But let's say we then have this happen:
public class Person {
private int age;
public int Age {
get { return age; }
set { age = value; }
}
public String WhatIsMyAge() {
return "My age is: " + ???;
}
}
void Main() {
Person alice = new Person();
alice.Age = 20;
Console.WriteLine(alice.WhatIsMyAge());
}
Is it better to use the property, or the field, here? Does it matter? Since we are inside the native class, do we still care about encapsulating the internals?