I have a simple interface with methods such as
bool TryGetValue(string key, out string value);
bool TryGetValue(string key, out int value);
bool TryGetValue(string key, out double value);
bool TryGetValue(string key, out DateTime value);
// only value types allowed
//with the implementation based on dictionary<string, object>
bool TryGetValue(string key, out string value)
{
object rc;
if ( dict.TryGetValue(key, out rc) )
{
value = rc.ToString();
return true;
}
value = null;
return false;
}
Looks like a perfect case for generics as
bool TryGetValue<T>(string key, out T value) where T: ValueType;
except not able to work out the func implementation, anyone ?
UPDATE - the following does not compile, i'd like to avoid creating multiple TryGet... funcs !
bool TryGetValue<T>(string key, out T value)
{
return dict.TryGetValue(key, out value) ;
}