tags:

views:

231

answers:

5

Is it possible to write in C# method in such a way that when I write

 String contestId = getParameter("contestId")

i get contestId in string, but when I write:

 int contestId = getParameter("contestId")

i get contestId parsed to integer?

This is only simple example showing what i try to achieve.

+15  A: 

Nope it's not possible to overload methods solely based on their return type. You could, however, introduce a generic parameter:

T getParameter<T>(string input) {
    // ... do stuff based on T ...
}

And if you were using C# 3.0 you could use this method as:

var str = getParameter<string>("contestid");
var integer = getParameter<int>("contestid");

thus saying the actual type only once.

Mehrdad Afshari
I misinterpreted this initially, so maybe some clarification as to what the generic parameter is may be useful?
workmad3
workmad: I added a code snippet. Do you think it requires further clarification? I'll try if you think it still needs it.
Mehrdad Afshari
+1 for being first and correct. =P
Tony
Mehrdad: That clarifies it a lot to me :)
workmad3
This is exactly what I was looking for. I'm coming to C# from Java background and didn't know that I can do stuff based on T type in method body. Thanks :)
mgamer
A: 
public T GetParameter<T>(string parameterName)
{
   //Do work
   return value
}

string contestId = getParameter<string>("contestId")
int contestId = getParameter<int>("contestId")

This is an example of your best bet.

Tony
+3  A: 

One thing you could do is return a separate object, which has implicit conversion operators to both int and string. That would get fairly close to the behavior you're asking for.

I wouldn't do that in practice though. Implicit conversions generally cause more trouble than they're worth.

Instead, add a generic parameter, like Mehrdad showed:

var str = getParameter<string>("contestid");
var integer = getParameter<int>("contestid");
jalf
+1 for mentioning the possibility. Although I wouldn't ever write something like that.
Mehrdad Afshari
+1  A: 

I prefer this approach, it reads nicely.

Public Class ResultProxy
{
  Private Object _Obj
  Public ResultProxy(Object O)
  { _Obj = O; }

  Public T As<T>()
  { return (T)_Obj; }
}

...

Public ResultProxy getParameter("contestId")
{
// your method's code
   return new ResultProxy(YourPersonalFavorateReturnType);
}

...

String s = getParameter("contestId").As<String>();
tom.dietrich
+1  A: 

Firstly the answer is no as many people have mentioned. Why? Do you have to assign the result of a method to something? For example can you have

int getValue()
{
  return 4;
}

getValue();

The answer is yes, it can, so there is no way for the compiler to know which method you intend to call by its return type.

Personal opinion here, but I would suggest something along the lines of

public string getContestIdAsString(string ConetestId);

public int getContestIdAsInt(string ContestId);

Very obvious what each one is doing and you get around your problem. Unless there is something that I am missing.

uriDium
+1, actuall all that stuff tith generics are error prone.Another recomendation is to use CLR types instead of c# (e.g. getContestIdAsInteger), this is generic recomendation that take roots from multi language nature of .NET. Int or bool will confuse VB developes, so Integer or Boolean is better.
Mike Chaliy
I agree if you want to hardcode the behavior based on a few different types but there are cases you want to do this for many types (like Enumerable.OfType<T> extension method). In that case, generic method shines.
Mehrdad Afshari