What you have implemented is known as an Automatic Property
they look like this:
private string Name { get; set; }
Automatic properties merely syntactical sugar and in reality, provide a succinct, quick way to implement this code:
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
You could disregard automatic properties, using manual proeprties and simply remove the get
. Or use automatic properties and make the property's value read only to external members by marking the get
with the private access modifier:
public string Name { get; private set; }
The code you say you would usually use, is never really needed in C# because properties, in reality are just methods in disguise and should be used as a better convention:
public int getId(){return id;} //bad