C#, dynamic return type

21,081

Solution 1

The return type of a function must be typed. As with any other variable or operation, any type that inherits from the specified type is a valid return value (which is why object allows anything as a value).

The logistics of the caller wouldn't make much sense; how would you know whether to type your variable as a char or a string in your example?

Is there a particular reason that you can't return a string in all cases?

Solution 2

I don't think it's theoretically possible. You could use "dynamic" in C# 4, but if you're not there yet, you can't do that.

If it could return either a string or a character, then it has to be an object that both of those descend from, of which "object" is your only choice. And then you'd have to do casts.

C# (3.5 and previous) needs to know what the single return type of the method is so that it can do checking on the calling method. And in fact, in C#4, when you say "dynamic" it actually uses "object" underneath.

You could create a custom class/struct that has both

public class StringOrChar
{
    char charValue;
    string stringValue;
    bool isString;
}

But it's kludgy.

Why do you want to have different return types?

Solution 3

there are alot of " left(str,1) < 'x' " but other times it is simply abc = left(str,3)

In this particular case, you could just return a single-character string. Comparing strings operates on their lexicographical order, so there's no specific need to use chars here (unless, of course, the original author was using polymorphism to treat the chars differently from strings, in which case you have my sympathies).

Solution 4

Your can use out parameters. or return a type that contains both variations.

Solution 5

methods can only - as far as i know - return one type

so return object and cast is the logical alternative

however, note that if your design has forced you into this predicament, there may be a flaw in it; perhaps some more details can help us help you

note: jon skeet is immune from all such restrictions

Share:
21,081
Enriquev
Author by

Enriquev

Updated on January 09, 2020

Comments

  • Enriquev
    Enriquev over 4 years

    What I need is a method that can return a type (no object, cause no casting allowed) following a condition. Here is an example:

    ??? right (string fullStr, int endPosition)
    {
     string tmpStr = "";
    
     tmpStr = fullStr.Substring(endPosition);
    
     if(tmpStr.Length == 1)
       return tmpStr[0]; //return a char!!
     else
       return tmpStr; //return a string!!
    }
    

    I tried generics but I was only able to return the type that was coming in, (if a char came in a char was returned, and if a string came in a string was returned). I tried this:

        public static T right<T>(T stringToCheck, int endPosition)
        {
            if (typeof(T).ToString() == "System.String")
            {
                string fullString = (string)((object)stringToCheck);
                string response = "";
    
                response = fullString.Substring(endPosition);
    
                if (response.Length == 1)
                {
    
                    return (T)((object)response[0]);
                }
                else
                    return (T)((object)response);
            }
    
            return stringToCheck;
        }
    

    I can't use typecasting (returning an object), cant use ref params.

    Calls to the method have to stay the same:

    right(stringOrChar,int) -> returns string or char.
    

    Thank You

  • Yinda Yin
    Yinda Yin over 14 years
    Jon Skeet also has shift keys and periods on his keyboard.
  • Steven A. Lowe
    Steven A. Lowe over 14 years
    @[Robert Harvey]: Jon Skeet does not need a keyboard; he has a direct neural interface courtesy of Google UK....and if e e cummings bothers you, feel free to edit. ;-)