-2

Why do people do this

int myInt { get; set; }

rather than just this?

int myInt;
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
cj32
  • 23
  • 6
  • 1
    The first is a property, the second is a field. Feel free to research on the difference between them. – Heretic Monkey Feb 18 '18 at 15:56
  • You should never make fields of a class visible to the outside. Field manipulation should only happen inside the class declaring the field. Access from outside only via properties. Also, using a property from the get-go makes modification easier when your myInt at some point has to do more than just store an int. Also you cannot use fields in interface definitions. – Dee J. Doena Feb 18 '18 at 15:58

1 Answers1

0

Some code will make it simple for you to start with.
What do you prefer?
this?:

private int _myInt;
public int GetMyInt()
{
    return _myInt;
}
public void SetMyInt(int value)
{
    _myInt = value;
}

or this?:

int myInt { get; set;}

Properties makes it easy. you can read more information here.