Not really understanding Backing Fields with custom get; and set;

Hey all.

I'm getting a little confused with backing fields with custom getters and setters. I have the following code
  internal class Person
  {
    private string _name;
    public string Name { get { return _name; } }

    private int _age;
    public int Age
    {
      get { return _age; }
      set
      {
        if (value >= 0)
          _age = value;
        else
          Console.WriteLine("Age cannot be below 0!");        
      }
    }

    public Person(string name, int age)
    {
      _name = name;
      Age = age;
    }

    public void GreetPerson()
    {
      Console.WriteLine($"Welcome, {_name}! You're {_age} years old!");
    }
  }

The thing that's confusing me is, how they're assigned within the constructor. I'll try to explain as best as I can.

If in the constructor, _name = name, which is using the backing field (I assume), how come for the age I need to do Age = age and not _age = age?, it feels like _age isn't being used at all.

Is it because of how _age is used in the set why Age is used in the constructor?
For example; if 5 is used on initialization of the object, the setter is setting _age to 5, then as we're getting _age via Age, this is why Age is used in the constructor and not _age? I hope that's correct!

Sorry if I've explained this poorly...
Was this page helpful?