I know this must be a common question, but take a look:
Here i have a test class:
public class EmployeeClass
{
private int _id;
private string _name;
private double _salary;
public int id
{
get{...}
set{...}
}
public string name
{
get{...}
set{...}
}
//and so on
}
The question is: for me, it doesnt make sense to have public properties matching ALL private fields.
What is the approach to limit the access to itens of the class?
At the time of initialization do i access the fields directly, as in:
public EmployeeClass(int id, string name, double salary)
{
_id = id;
_name = name;
_salary = salary;
}
AND MAKE ALL (at least the ones that must have some sort of immutability) readonly?
What’s the best approach here?
Thank you for your opinions
There are many attitudes, two of them that helps avoiding getters on the class are:
In general (and this is only a tiny taste), it is not recommended to have many public setters on the class.