I'm new to C# coming from a Java background. I'm working on a take home assignment for a coding interview. I normally write my code as following(Java):
public class Test {
//fields
private string fieldA;
private int fieldB;
public Test () {
//.....
}
public String getFieldA() {
return fieldA;
}
//ETC.
}
The point is that I use getters/setters like above. Now, I have a coding assignment to do for a job interview, and using C# I have stumbled across automatic properties. I was initially slightly confused about it but understand what it does now and have used it.
So for fields I would write
public string fieldA {get; set;}
It feels weird declaring fields public like that and doesn't it violate some OOP encapsulation principles ?
My question is, should I just go ahead and use automatic properties to show interviewers that I know what it is? In case I want to prevent setting a field I could just use:
public string fieldA {get; private set;}
But would it appear like I don't follow encapsulation "rules", I obviously wouldn't want to risk that. I could write it out as I do in Java.
In general what is your view on using automatic properties ?