Int is initialized to 0 by default; Assuming you don't want to use int? which is perfect for you. You can check this, or you can have a flag and a support field:
private int _age; public int Age { get { return _age; } set { _age = value; _hasAge = true; } } public bool HasAge { get { return _hasAge; } }
As suggested above, you can initialize it to an invalid state:
private int _age = -1; public int Age { get { return _age; } set { _age = value; _hasAge = true; } } public bool HasAge { get { return _age != -1; } }
Or just break and use int?
public int? Age { get; set; } public bool HasAge { get { return Age.HasValue; } }
For backward compatibility with your code, can you disable its int? without exposing it to:
private int? _age; public int Age { get { return _age.GetValueOrDefault(-1); } set { _age = value; } } public bool HasAge { get { return _age.HasValue; } }
Arena
source share