I've just gotten started learning c#, and I'm struggling a bit with the getter and setter shorthand.
As I understand it, the two methods below are equivalent. Is this correct?
//Method 1
public string name { get; set; }
//Method 2
private string name
public string getName() { return name;}
public string setName(string newName) { this.name = newName; }
Secondly, how does this work if we wanted different access modifiers on the getter/setter and the instance variable. The following code errors, telling me that the accessor must be more restrictive than the property and that I can't specify modifiers for bother accessors. Can somebody please clarify?
private int maxTime { public get; public set; }
EDIT: To clarify, I have no specific goal, only to understand. I don't understand what this shorthand notation does. In other languages I've had private instance variables and used public getters and setters to manage these instance variables. It allows this if I write out the methods on my own, but not with this shorthand notation. Why is this?
EDIT2: One last question to check my understanding. Both of the code snippets below use properties to manage the maxTime variable. The only difference between the two is style. Is this correct?
private int maxTime;
public int MaxTime{ get; set; }
vs
private int maxTime;
public int MaxTime
{
get { return maxTime; }
set { maxTime= value; }
}