The code you wrote is nonsense, because Console.ReadLine
always returns a string
(it is its return type after all!).
To answer your question, the is
operator is not equivalent to the GetType() == typeof()
statement. The reason is that is
will return true if the object can be cast to the type. In particular, it will return true for derived types, which would fail the other check. From MSDN:
An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered.
If you are looking for a specific type of input (say a number) then you need to try and Parse
or TryParse
it into that type. Something like:
double output;
if (double.TryParse(answer, out output)
{
//Its a number!
}
else
{
//Its some regular string
}
Without seeing more its impossible to say what exactly you need to write though.