0

I was reading up on some C# and came across this snippet of code from: http://blog.codinghorror.com/properties-vs-public-variables/

The code was:

private int _name;

public int Name
{
    get { return _name; }
    set { _name = value; }
}

Can you please explain what is going on in this code snippet here? Thanks!

TheSQLLearner
  • 51
  • 1
  • 8
  • You can add/do a lot more things in the `setter` and `getter` of the `properties` which you could not do in `fields`. Examples: access other properties fields, call functions, raises events, etc... in fields, you only set value and get value of the variable. – Ian Mar 02 '16 at 13:10
  • The field `_name` holds the value from the property `Name`. In this case, you could manipulate the value you would get through the getter and setter. – Christoph K Mar 02 '16 at 13:11
  • Also realize the article is 10 years old. We didn't even have auto-properties back then. – Dennis_E Mar 02 '16 at 13:21

0 Answers0