I have a method like:
public T Get<T>(string key)
{
}
Now say I want to return "hello" if the type is a string, and 110011 if it is type int.
how can I do that?
typeof(T) doesn't seem to work.
I ideally want to do a switch statement, and return something based on the Type of the generic (string/int/long/etc).
Is this possible?
The following should work
public T Get<T>(string key) {
object value = null;
if ( typeof(T) == typeof(int) ) {
value = 11011;
} else if ( typeof(T) == typeof(string) ) {
value = "hello";
}
return (T)value;
}
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