C
C#2y ago
joy

c sharp property validation if value is null

hi all, im trying to do a properties validation if a value for my property for example userName is null then i will return an empty string instead of a null value but if i have lots of properties for an object, do we have to do it like below validation (redundancy), im wondering if this is a good way, can anyone help to advise? thank you so much in advance..
private string _userName;
public string userName {
get { return _userName; }
set {
if (string.IsNullOrEmpty(value)) _userName = string.Empty;
else _userName = value;
}
}
private string _userName;
public string userName {
get { return _userName; }
set {
if (string.IsNullOrEmpty(value)) _userName = string.Empty;
else _userName = value;
}
}
2 Replies
Auger
Auger2y ago
I think how you have it in the setter is the right place to put that behavior
joy
joy2y ago
ohh.. okay understand.. thank you!!