title: C#, dynamic return type
body: <p>What I need is a method that can return a type (no object, cause no casting allowed) following a condition. Here is an example:</p>
  
  <pre><code>??? 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!!
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>    public static T right&lt;T&gt;(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;
      }
  </code></pre>
  
  <p>I can't use typecasting (returning an object), cant use ref params.</p>
  
  <p>Calls to the method have to stay the same:</p>
  
  <pre><code>right(stringOrChar,int) -&gt; returns string or char.
  </code></pre>
  
  <p>Thank You </p>