Udostępnij za pośrednictwem


Fun with Equality

Isn’t it funny how the “easiest” concepts can be the most complicated? A reader sent me the following quiz to help us appreciate the subtlies of equality in the system. Luckily he gave me the answers as well… ;-)

Consider the following program:

object x = new object();

object y = new object();

/*Question 1*/ Console.WriteLine(x == y && !x.Equals(y));

/*Question 2*/ Console.WriteLine(x != y && x.Equals(y));

/*Question 3*/ Console.WriteLine(x == y);

/*Question 4*/ Console.WriteLine(x == y && (object)x != (object)y);

They should all print “false” right?

For each question, find a way to declare and initialize x and y such that:

            Question 1: prints true

            Question 2: prints true

            Question 3: does not even compile

            Question 4: prints true

Notice, we are not asking for a solution that works for all 4, one solution for each will be fine…

All answers should be of the form:

         public static void Question<<question number>>()

        {

            <<type>> x = <<intialize instance>>;

            <<type>> y = <<intialize instance>>;

            Console.WriteLine(<<question>>);

        }

So for example, a legal (but incorrect) answer to question1 would be:

         public static void Question1()

     {

            string x = "1";

            object y = new Object() ;

            Console.WriteLine(x == y && !x.Equals(y));

        }

The rules of the game:

  1. You can only use “base” data types: object, string, Int32, double, Type, etc
  2. No more than two lines of plainly formatted code (before the Console.WriteLine)
  3. You can’t change anything about the question line, it has to appear exactly as I show above
  4. Extra credit for getting solutions no one else thinks of and for pointing out differences between versions of the CLR

Comments

  • Anonymous
    April 03, 2005
    public static void Question1() {
    int x = 1;
    long y = 1;
    Console.WriteLine(x == y && !x.Equals(y));
    }
    public static void Question2() {
    double x = double.NaN;
    double y = double.NaN;
    Console.WriteLine(x != y && x.Equals(y));
    }
    public static void Question3() {
    Char x = 'a';
    String y = "a";
    Console.WriteLine(x != y && x.Equals(y));
    }
    public static void Question4() {
    int x = 1;
    long y = 1;
    Console.WriteLine(x == y && (object)x != (object)y);
    }

  • Anonymous
    April 03, 2005
    // Question 1:
    public static void Question1()
    {
    int x = 5;
    float y = 5f;
    Console.WriteLine(x == y && !x.Equals(y));
    }

    In 2.0, you've got the various int.Equals, float.Equals, etc. methods, whereas in previous versions these Equal methos were just calling the Object.Equals method.

    // Question 2:
    public static void Question2()
    {
    object x = 5.ToString();
    string y = 5.ToString();
    Console.WriteLine(x != y && x.Equals(y));
    }

    // Question 3:
    public static void Question3()
    {
    int x = 5;
    object y = new object();
    Console.WriteLine(x == y); // Can't compare int and object
    }

    // Question 4:
    public static void Question4()
    {
    int x = 5;
    int y = 5;
    Console.WriteLine(x == y && (object)x != (object)y);
    }

  • Anonymous
    April 03, 2005
    //My solutions, which play with Nullable<T> and String (and Nullable<String>)

    //If you switch the data types (int? x and int y) then this returns false
    public static void Question1() {
    int x = 0;
    int? y = 0;
    Console.WriteLine(x == y && !x.Equals(y));
    }

    //Reference equality vs value equality
    public static void Question2() {
    object x = "";
    object y = String.Copy("");
    Console.WriteLine(x != y && x.Equals(y));
    }

    // Operator '==' cannot be applied to operands of type 'string?' and 'string'
    // I'm not sure why this is the case, but C# doesn't seem to lift operators when T is a reference type
    public static void Question3() {
    string? x = "";
    string y = "";
    Console.WriteLine(x == y && (object)x != (object)y);
    }

    // This one acts returns false on .NET 1.1,
    // but mscorlib in 2.0 does not intern its own strings, including String.Empty
    public static void Question4() {
    string x = String.Empty;
    string y = "";
    Console.WriteLine(x == y && (object)x != (object)y);
    }

  • Anonymous
    April 03, 2005
    public static void Question1()
    {
    char x = 'X';
    int y = 88;
    Console.WriteLine(x == y && !x.Equals(y));
    }

    public static void Question2()
    {
    object x =true;
    object y = true;
    Console.WriteLine(x != y && x.Equals(y));
    }
    public static void Question3()
    {
    bool x =true;
    string y = "true";
    Console.WriteLine(x == y);
    }
    public static void Question4()
    {
    int x =1;
    int y = 1;
    Console.WriteLine(x == y && (object)x != (object)y);
    }

  • Anonymous
    April 04, 2005
    This is a test

  • Anonymous
    May 10, 2005
    public static void Question1()
    {
    int x = 10;
    long y = 10;
    Console.WriteLine(x==y && !x.Equals(y));
    }
    public static void Question2()
    {
    object x = 10;
    object y = 10;
    Console.WriteLine(x!=y && x.Equals(y));
    }
    public static void Question3()
    {
    object x = new object();
    int y = 10;
    Console.WriteLine(x==y);
    }
    public static void Question4()
    {
    int x = 10;
    int y = 10;
    Console.WriteLine(x==y && (object)x!=(object)y);
    }

  • Anonymous
    July 01, 2005
    /* Personal comment: great quiz :-) */
    using System;

    class Quiz
    {
    public static void Main()
    {
    Question1();
    Question2();
    Question3();
    Question4();
    }

    public static void Question1()
    {
    int x = 1;
    double y = 1;
    Console.WriteLine(x == y && !x.Equals(y));
    }

    public static void Question2()
    {
    object x = 1;
    object y = 1;
    Console.WriteLine(x != y && x.Equals(y));
    }

    public static void Question3()
    {
    int x = 1;
    string y = "1";
    Console.WriteLine(x == y);
    }

    public static void Question4()
    {
    int x = 1;
    double y = 1.0;
    Console.WriteLine(x == y && (object)x != (object)y);
    }
    }

  • Anonymous
    October 09, 2006
    PingBack from http://aleemkhan.wordpress.com/2005/05/11/fun-with-equality/

  • Anonymous
    March 19, 2008
    PingBack from http://cityjokesblog.info/brad-abrams-fun-with-equality/

  • Anonymous
    June 15, 2009
    PingBack from http://debtsolutionsnow.info/story.php?id=603

  • Anonymous
    June 16, 2009
    PingBack from http://fixmycrediteasily.info/story.php?id=6249