독학으로 공부 해 오면서 최근부터 c# 버전을 업을 했더니 가능한 null 참조반환이라는 경고가 떠서 검색해봤더니 null 허용 연산자 ?, ?? 을 사용하면 경고가 없어지긴 했습니다. 현업에서는 어떻게 사용하고 처리하시는지 궁금하더군요.
이런 코드 예시를 가져와봤습니다.
멤버변수나 매개변수에 ?연산자를 붙여봤지만 리턴하거나 다른 함수에 대입될땐 어김없이 경고가 뜨네요
어떻게들 사용하고 계신지요? 안쓰시는분도 계신가요??
private string? _password;
[Required(ErrorMessage = “Password is Required”)]
public string Password
{
get { return _password; }
set
{
_name = value;
Validate(nameof(Password), value);
}
}
public IEnumerable GetErrors(string? propertyName)
{
if (Errors.TryGetValue(**propertyName**, out List<string>? value))
{
return value;
}
else
{
return Enumerable.Empty<string>();
}
}
public void Validate(string? propertyName, object propertyValue)
{
var results = new List();
Validator.TryValidateProperty(propertyValue, new ValidationContext(this) { MemberName = propertyName}, results);
if (results.Any())
{
Errors.Add(propertyName, **results.Select(x => x.ErrorMessage).ToList()**);
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
}
else
{
Errors.Remove(propertyName);
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
}