Hi all,
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