How to chack if an object is equal to the current object in C#

2 Answers

0 votes
using System;

public struct Worker
{
    private string firstname;
    private string lastname;

    public Worker(string fname, string lname)
    {
        this.firstname = fname;
        this.lastname = lname;
    }
}

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Worker w1 = new Worker("Karoline", "Trampoline");
                Worker w2 = new Worker("Nicole", "roll");

                Worker w3 = new Worker("aaa", "bbb");
                Worker w4 = new Worker("aaa", "bbb");

                Console.WriteLine(w1.Equals(w2)); // False
                Console.WriteLine(((object)w1).Equals((object)w2)); // False                 

                Console.WriteLine(w3.Equals(w4)); // True
                Console.WriteLine(((object)w3).Equals((object)w4)); // True 
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


/*
run:

False
False
True
True

*/

 



answered Aug 1, 2015 by avibootz
edited Aug 1, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            long v1 = 13;
            int v2 = 13;

            try
            {
                object o1 = v1;
                object o2 = v2;

                Console.WriteLine(o1.Equals(o2)); // False
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}


/*
run:

False

*/

 



answered Aug 1, 2015 by avibootz
...