1

I found in one book this way to declare variables:

class JerseyNumber
{
    public string Player { get; private set; }
    public int YearRetired { get; private set; }

    string JerseyNumber(string player, int numberRetired)
    {
        Player = player;
        YearRetired = numberRetired;
    }
}

What will change if I'll remove the code:

{ get; private set; }
Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • Also: [Difference between Property and Field in C# 3.0+](http://stackoverflow.com/questions/653536/difference-between-property-and-field-in-c-sharp-3-0) – poke Mar 01 '14 at 00:54

2 Answers2

4

What will change if I'll remove the code:

Then you will make it a field instead of a auto-implemented property.And you lose your private setter, so the value of the Player can be changed from outside of the class (for example, the same thing applies for YearRetired property)

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • +1. Note that both of this changes are not good practices as it generally better to prefer properties to fields, and prefer immutable objects to mutable ones if possible. – Alexei Levenkov Mar 01 '14 at 00:51
1

By removing the {get; private set;} you are no longer using Automatic Properties. You will then need to provide the code to return and set those values.

private string _player = "";
public string Player
{
   get { return _player; }
   set { Player = value; }
}
Eddie
  • 466
  • 5
  • 8