Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, dynamic return type

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

like image 269
Enriquev Avatar asked Nov 23 '09 19:11

Enriquev


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


2 Answers

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?

like image 177
McKay Avatar answered Sep 28 '22 07:09

McKay


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?

like image 22
Adam Robinson Avatar answered Sep 28 '22 06:09

Adam Robinson