When we are unsure about what
Type
a function will return, we should use
Object
in that scenario.
you can achieve this without generic. look at the following code.
protected void Page_Load(object sender, EventArgs e)
{
object ret = method(0);
if (ret.GetType() == typeof(string))
{
Response.Write("String Object Error");
}
else
{
Response.Write("List Object");
}
}
object method(int input)
{
if (input == 0)
{
return new List<int>();
}
else
{
return "Error in input";
}
}
you may use polymorphism concept also. you make return type of Type
Base
and return the
derived
types as well. test the type and do accordingly.
--------------------------------------------------------------------------------------------
Return types are pre-defined at the time of declaration of the
Method
. you cannot change its
Type
at run-time using the same function and within the function. return
Type
can be changed at run-time but not from within the Method.
In generic the return
Type
is passed along with the Method call like below.
T method<t>(int a)
{
T t1
return t1;
}
bool c = method<bool>(1);
</bool></t>
here the return type is passed when we are calling the method. we cannot decide the Type T from with in the method like .
method(int a)
{
return "ass";
.
.
return new List<payment>();
}
</payment>