views:

128

answers:

5

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?

+4  A: 

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;
}
JaredPar
Check the `typeof()` comparison on your `else if.`
Anthony Pegram
@Anothony, thanks fixed the typo.
JaredPar
And should perhaps `value` be initialized to `default(T)` rather than `null` to avoid the `NullReferenceException` in the event that `T` does not exist within the `if-else if` structure?
Anthony Pegram
@Anthony, that's a valid suggestion. In this case though the author didn't specify the behavior on an unexpected type so it probably would be best to just explicitly through :)
JaredPar
I thought to myself that the NRE might be good since the behavior wasn't defined within the `if-else if`, but then I considered the inconsistency of it since reference types could be unhandled and *not* throw.
Anthony Pegram
+4  A: 

I have to wonder what the real intent of the question is... Is the Get method supposed to retrieve a value of the specified key from some kind of storage? In that case, I would imagine something like

public T Get<T>(string key)
{
    // GetValue(key) returns a value of type object.
    return (T)_storage.GetValue(key);
}

would be more appropriate. The answers proposed so far make no use of the key parameter at all (I suppose that is legitimate, given that the original question seeks to hard code some kind of return value based on the type of T, regardless of what key represents).

You normally use generics to provide a generic behavior regardless of the type you're dealing with. If you start introducing a switch statement to modify behavior based on the Type of the method, you likely aren't using generics properly, and/or you should look into implementing separate, type-specific methods.

Phong
+2  A: 

Since T is not a parameter to this method, it cannot be deduced from usage. So, your current usage is:

int x = obj.Get<int>("key");

In that case, why not just return object? Your usage would be:

int x = (int)obj.Get("get");
Stephen Cleary
+3  A: 

Personally, I think this is a bad pattern for generic methods.

Generics are supposed to support "generic" operations across different object classes where the underlying type is immaterial. Excluding, of course, when you constrain it down to one branch of a class hierarchy... At which point you might even be better off just adding the method to the parent class via direct editing of the parent class code or extension methods.

The minute you start adding type specific code to a generic method is the minute you've gone out of bounds and need to reconsider whether a type specific method is better for your situation.

Chris Lively
+2  A: 

If you're using a switch statement to triage by Type then your method is likely not a good candidate for a generic.

Mark