I want a typed lookup helper function for a heterogenous collection: It should return a struct or class, else null if the item is not found. Below is an example using a trivial collection lookup, but it could be a database call or whatever.
Is there any way to achieve this with a single method signature?
public T GetClass<T>(string key) where T : class
{
object o;
if (Contents.TryGetValue(key, out o))
{
return o as T;
}
return null;
}
public T? GetStruct<T>(string key) where T : struct
{
object o;
if (Contents.TryGetValue(key, out o))
{
if (o is T)
{
return (T?) o;
}
}
return null;
}
What I've already tried:
(Default) T
isn't an option, since 0 is a valid int value.<int ?>
as the type, but as discussed, Nullable<T>
isn't a reference type.Is there some way to indicate that I'm going to return a boxed int?
Is there any way to achieve this with a single method signature?
There's a horrible (truly hideous) way to do it using optional parameters so that the calling code can look the same in both cases. It's icky though.
Options:
Tuple<T, bool>
instead of using nullityout
parameter (like int.TryParse
etc)Note that by signalling the absence of a value separately, you can make null
a valid "found" result, which can be useful sometimes. Or you may want to just guarantee that it'll never be returned.
If you really want to use nullity, I'd go for the last option. I believe it will make your code clearer anyway. IMO, overloading should really only be used when the methods do exactly the same thing expressed using different parameters - whereas returning Nullable<T>
as the return type in one case and T
as the return type in the other case can't really be seen that way.
The following method works for both classes and nullable structures:
public static T GetValue<T>(string key)
{
object o;
if (Contents.TryGetValue(key, out o))
{
if (o is T)
{
return (T)o;
}
}
return default(T);
}
Usage:
int? result1 = GetValue<int?>("someInt");
string result2 = GetValue<string>("someString");
Notice how the ?
is part of generic type argument and not defined by the method on the return type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With