DefaultValue
I find that checking whether or not a map contains a key can be tedious. Sometimes, a non-existent key should produce a simple error value, such as null, zero, or even an enum.undefined. Let us consider the following Dictionary blunder:
public enum StatusType
{
Undefined,
Enabled,
Disabled,
}
public Dictionary<string, StatusType> MembershipStatus { get; set; }
///ctor
MembershipStatus = new Dictionary<string, StatusType>();
///
public StatusType GetStatus(string name)
{
if (!MembershipStatus.ContainsKey(name))
{
return StatusType.Undefined;
}
return MembershipStatus[name];
}
///
var status = GetStatus("joe");
///
Among many things, my irritation with this code is that we had to add a function just to help out this miserable dictionary. Lame! Use a map.
public enum StatusType
{
Undefined,
Enabled,
Disabled,
}
public Map<string, StatusType> MembershipStatus { get; set; }
///ctor
MembershipStatus = new Map<string, StatusType>();
MembershipStatus.DefaultValue = StatusType.Undefined;
///
//now there is no need for GetStatus!
///just use the map
var status = MembershipStatus["joe"];
///