开发者

How to return variable datatypes from a method

开发者 https://www.devze.com 2022-12-13 01:46 出处:网络
I have a method whose callers require different datatypes.I could repeat the requisite conversions a开发者_运维百科 dozen times by putting them in each of the callers, but it seems much more efficient

I have a method whose callers require different datatypes. I could repeat the requisite conversions a开发者_运维百科 dozen times by putting them in each of the callers, but it seems much more efficient to do it once in the called method.

public String myMethod(String myArg)
{    
    return DoSomething(myArg);
}

public Int32 myMethod(String myArg)
{
    return Convert.ToInt32(DoSomething(myArg));
}

private String DoSomething(key)
{
    return SomeList[key];
}

If I have multiple methods that pull data from SomeList and have to utilize different data types then in each I have to do a type conversion. Examples might be session variables or query or form requests or any other number of things.

In VB I could say

Function myMethod(myArg as String) as Variant
    myMethod=DoSomething(myArg)
End Function

Sorry if the original post was not very clear. I hope this makes more sense.

Thanks


public T MyMethod<T>(string myArg)
{
 //do work here
}

USAGE

  public T MyMethod<T>(string myArg)
    {
        return (T)Convert.ChangeType(DoSomething(myArg), typeof(T));
    }

A bit over engineered maybe ;)


The only way this is possible is to return an "object" reference and then infer type in the calling code. As the previous poster said C# functions cannot vary by return type only.

For example:

private object myMethod(String myArg)
{    
    if (a == true)
       return "StringResult";
    else if (b == true)
       return 27;
    else
       return 1.4f;
}

private void Caller()
{
   object result = myMethod("arg");

   if (result is String)
      // Do string stuff
   else if (result is Int32)
      // Do int stuff
   else if (result is Single)
      // Do float stuff     
}


You can't have two methods with the same name and argument list, only differing in return type. No way.


To return different types from a method the return types must all share a common class in their inheritance hierarchy, such as object.

Section "10.6 Signatures and overloading" of the C# Language Specification 4th edition addresses overloading. Relevant excerpts:

"The signature of a method consists of the name of the method, the number of type parameters, and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. The signature of a method specifically does not include the return type, parameter names, or type parameter names, nor does it include the params modifier that can be specified for the right-most parameter. When a parameter type includes a type parameter of the method, the ordinal position of the type parameter is used for type equivalence, not the name of the type parameter."

"Overloading of methods permits a class, struct, or interface to declare multiple methods with the same name, provided their signatures are unique within that class, struct, or interface."

Section "10.2 Overloading" of the Common Language Infrastructure 4th edition also addresses the topic. Relevant excerpts:

"CLS Rule 38: Properties and methods can be overloaded based only on the number and types of their parameters, except the conversion operators named op_Implicit and op_Explicit, which can also be overloaded based on their return type."


What Variant was in VB, object is in C#:

object Foo(int n) {
    switch (n) {
    case 1: return 123;     // can return an int
    case 2: return 123.456; // ... or a double
    case 3: return "foo";   // ... or a string
    case 4: return true;    // ... or a boolean
    ...
    default: return new MyClass();    // ... or anything, really
}


yet another option, use an out parameter rather than the return value.

void Foo(string args, out string answer)
{

}
void Foo(string args, out int answer)
{

}
0

精彩评论

暂无评论...
验证码 换一张
取 消