Possible Duplicate:
Difference between Property and Field in C# .NET 3.5+
Why should I use an automatically implemented property instead of a field?
Both Examples below do exactly the same, have identical access writes within and outside of the class... So why does everyone seem to use Example 1 as apposed to Example 2? I'm sure I'm just missing something, but this has been bugging me for a while now and I haven't been able to find a clear cut answer.
class SampleClass
{
/// Example 1
/// Shown by online examples.
/// Why use a Field AND a Property where you could just use Example 2?
private int age;
public int Age { get { return age; } }
private void setAge()
{
age = 1;
}
/// Example 2
/// Tidier code and better understanding of how Age2 can be accessed.
/// Personally I prefer this method, though am I right to use it over Example 1?
public int Age2 { get; private set; }
private void setAge2()
{
Age2 = 1;
}
}