Is this both the same:
private int x;
public int X
{
get { return x; }
set { x = value; }
}
and
public int X
{
get { return x; }
set { x = value; }
}
I mean is it enough to use the second case or there are special cases I should use the first case?
because I sometimes see codes with first case and sometimes with second case, so i have confused
Your 2nd case wont compile – as ‘x’ (lowercase) isn’t defined.
The other option is to use an ‘automatic property’
All this does, is make the compiler automatically generate a backing field behind the scenes, that you can’t directly access, but is mostly the same as your first example.