17

I am learning C#, and am learning about making fields private to the class, and using Getters and Setters to expose Methods instead of field values.

Are the get; set; in Method 1 and Method 2 equivalent? e.g. is one a shorthand of the other?

class Student
{
    // Instance fields
    private string name;
    private int mark;

    // Method 1
    public string Name { get; set; }

    // Method 2
    public int Mark
    {
        get { return mark; }
        set { mark = value; }
    }
}

Finally, would Method 2 be used when you want to for example perform a calculation before getting or setting a value? e.g. converting value to a percentage or perform validation? e.g.

class Student
{
    // Instance fields
    private string name;
    private double mark;
    private int maxMark = 50;

    // Method 1
    public string Name { get; set; }

    // Method 2
    public double Mark
    {
        get { return mark; }
        set { if ( mark <= maxMark ) mark = value / maxMark * 100; }
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Gravy
  • 12,264
  • 26
  • 124
  • 193
  • Have a look at [this answer](http://stackoverflow.com/questions/15454470/why-arent-simple-properties-optimized-to-fields/15454538#15454538); it shows something in detail. – Ken Kin May 25 '13 at 18:29
  • possible duplicate of [What are Automatic Properties in C# and what is their purpose?](http://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose) – Kirk Woll May 25 '13 at 19:50

2 Answers2

15

Yes, the Method2 is the way to go when you have a custom getter and setter function. By default when you use Method1, there will be a default private property handled internally. Please refer this URL for more details.

Sample:

string _name;

public string Name 
{
    get => _name;
    set => _name = value;
}
Saravanan
  • 7,637
  • 5
  • 41
  • 72
  • Is there a way to use default private properties but have a manual getter or setter for it? Also, that link is dead. – Aaron Franke Jan 14 '19 at 09:25
  • Yes, the approach is to have a private variable that is get and set using a public property or a function. I have updated a working link and added small sample code FYR – Saravanan Jan 16 '19 at 10:25
12

Yes, Method 1 is a shortcut to Method 2. I suggest using Method 1 by default. When you need more functionality, use Method 2. You can also specify different access modifiers for get and set.

Robin
  • 2,278
  • 3
  • 26
  • 46
  • 1
    Aah, so you mean that I can do something like `protected set { if ( mark <= maxMark ) mark = value / maxMark * 100; }`??? – Gravy May 25 '13 at 18:24
  • 2
    Note that the Name getter/setter will not return/set the private string name, but an internal variable which you do not have direct access to. – Bikonja May 25 '13 at 18:27
  • 1
    Thanks, And am I correct in saying that I do not have to explicitly declare the fields `name` or `mark` - but I can just reference the Methods `Name` and `Mark` within the class / or outside the `Student` class? – Gravy May 25 '13 at 18:36
  • Yes, that is correct. Name and Mark are automatic properties so there is no backing field. You can reference them directly from within the class. If they are public you can reference them from outside the class as well. – Robin May 27 '13 at 14:09
  • @Robin This is false. If you delete `private int mark;` from the program, it throws errors. – Aaron Franke Jan 14 '19 at 09:25