I was trying to help my friend to understand things such as fields and properties, and getters/setters. He then used properties without a private field, and told me it worked. I never even knew this was possible and can't seem to find too much about it online.
As an example:
public int Number { get; set; }
Instead of:
private int number;
public int Number
{
get { return number; }
set { number = value; }
}
It seems to work (as far I can see), but now I have the following questions:
- What is happening behind the scenes?
- What way is prefered?
- Any pros/cons?
EDIT:
I always thought { get; set; } was the same as get { return x; } set { x = value; }. Oh well, good that I now know it. The question however remains the same. Is one considered better than the other? Pros/Cons?