次の方法で共有


An "is" operator puzzle, part one

It is possible for a program with some local variable x:

bool b = x is FooBar;

to assign true to b at runtime, even though there is no conversion, implicit or explicit, from x to FooBar allowed by the compiler! That is,

FooBar foobar = (FooBar)x;

would not be allowed by the compiler in that same program.

Can you create a program to demonstrate this fact? This is not a particularly hard puzzle but it does illustrate some of the subtleties of the "is" operator that we'll discuss in the next episode.

Comments

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    We're having a fun time trying to solve this in the C# StackOverflow chat room.  Thanks for the puzzle Eric!

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    Gah, Ignore that, I misread the problem.

  • Anonymous
    August 23, 2012
    static void M<T>(T x)        {            bool b = x is FooBar;            FooBar foobar = (FooBar)x;        }

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    I, too, tested my code in LINQPad. Just realized that it does not produce a compiler error, but a RuntimeBinderException exception at runtime.

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    I think any developer who's attempted generic math has run into this: public void DoMath<T>(T value) {    if (value is int)    {        DoIntMath((int)value);    } }

  • Anonymous
    August 23, 2012
    The comment has been removed

  • Anonymous
    August 23, 2012
    Nope, forget it...it's an alias, thus the type is the same, and any cast/conversion would be allowed.

  • Anonymous
    August 23, 2012
    var x = new TypedReference(); bool b = x is object; Console.WriteLine(b); object o = (object)x;

  • Anonymous
    August 23, 2012
    Maybe this?    class Program    {        static void Main(string[] args)        {            dynamic x = new FooBar();            bool b = x is FooBar;            Console.WriteLine(b);            Console.ReadLine();        }        class FooBar        {        }    }

  • Anonymous
    August 23, 2012
    Eric, I can not wait to see a correct answer. Who is that lucky guy who guessed?

  • Anonymous
    August 24, 2012
    int x = 0; bool b = x is FooBar; Console.WriteLine(b); FooBar foobar = (FooBar)x;

  • Anonymous
    August 24, 2012
    namespace IsConsoleApplication {    class Program    {        static void Main(string[] args)        {            var x = new object();            var b = x is FooBar;            var fooBar = (FooBar)x;        }        class FooBar        {        }    } } 'object' is compatible with 'FooBar', so b = true; but 'x' don't refer to a 'FooBar' when the type verification is performed in the type hierarchy.

  • Anonymous
    August 25, 2012
    The comment has been removed

  • Anonymous
    August 25, 2012
    I agree with Stuart (comment #1).

  • Anonymous
    August 26, 2012
    The comment has been removed