1

So when calling Console.Readline and assigning that to a variable to be evaluated by an if statement I want to know if the following code is interchangeable and if not how are they different to the point I should pick one over the other for my application.

//code omitted

var answer = Console.ReadLine();

if (answer.GetType() == typeOf(string))
{
Console.WriteLine("Awesome");
}
// code omitted

just basically wondering if using that over

if (answer is string) ...

is the better choice?

RenniePet
  • 11,420
  • 7
  • 80
  • 106
DtechNet
  • 162
  • 3
  • 12
  • 9
    Why you are checking if string is string? It's more efficient to use `if (true)` :) – Sergey Berezovskiy Jun 20 '14 at 23:17
  • 7
    `Console.ReadLine()` always returns a string. – itsme86 Jun 20 '14 at 23:17
  • 2
    I'd recommend to read about `var` keyword: https://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp – alex.b Jun 20 '14 at 23:19
  • "how are they different"...they aren't. It's a nonsense question, in fact. "I should pick one over the other"...no, you pick neither. It's not sensible to test a know return type to be _of the return type_. You test unknowns, not knowns. – DonBoitnott Jun 20 '14 at 23:32
  • @DonBoitnott, the two tests look similar, so if you disregard the nonsense about the return of Console.WriteLine, its actually a good question. – BradleyDotNET Jun 20 '14 at 23:35
  • 1
    I'm sufficiently old-fashioned that I still refuse to use "var". – RenniePet Jun 21 '14 at 00:52
  • @RenniePet something tells me you'll learn. Our CTO was the same way 6 months ago and now he uses var for more things than I do :) – jebar8 Jun 21 '14 at 01:42
  • 2
    @BradleyDotNET It would be a good question if it weren't a duplicate of others such as http://stackoverflow.com/questions/983030/type-checking-typeof-gettype-or-is?rq=1 – ClickRick Jun 21 '14 at 02:24
  • 1
    @ClickRick - I suspect that real question asked (and answered) is "how to check if user entered number or string" (or something along this lines)... Would be nice if OP edit title to match the intent/answer. – Alexei Levenkov Jun 21 '14 at 02:37
  • 1
    @AlexeiLevenkov Agreed! – ClickRick Jun 21 '14 at 02:39

1 Answers1

5

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.

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • I just added a comment to that effect. And then deleted it, because `string` in particular is `sealed` so... :) – Jon Jun 20 '14 at 23:35
  • @Jon, writing `is string` is obviously uncommon, but the difference between the tests is more general, and important to note. I actually liked your comment (saw it after I posted my answer :) ) – BradleyDotNET Jun 21 '14 at 00:11
  • 1
    “`is` will return true if the object can be cast to the type” That's not accurate. You can cast `int` to `double`, but `42 is double` is `false`. – svick Jun 21 '14 at 01:20
  • @svick - good point. MSDN covers list of conversions considered by `is` to clarify it. – Alexei Levenkov Jun 21 '14 at 01:49
  • @BradleyDotNET - I've added one more sentence from MSDN that clarifies what "cast" means for `is` for svick's comment. Feel free to edit/revert. – Alexei Levenkov Jun 21 '14 at 01:50
  • @AlexeiLevenkov, thanks for the edit and comment. I don't use `is` for value types so I forgot about that case. – BradleyDotNET Jun 21 '14 at 02:17