Are these objects equal?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ganeshshegde
    New Member
    • Jun 2007
    • 8

    Are these objects equal?

    Hi, i want to compare two objects. i wrote the program as bellow,
    ............... .........
    namespace TestPrograms
    {
    class test
    {
    public int i;
    }
    class Exercise
    {
    public static void Main(string[] s)
    {
    test t1=new test();
    test t2=new test();

    t2.i=10;
    t1.i=10;

    if(t1.Equals(t2 ))
    {
    Console.WriteLi ne("Eql");
    }
    else
    {
    Console.WriteLi ne("NotEql");
    }
    }
    }

    }
    ............... .....
    O/P is "NotEql"
    ............... ...
    my question is ,
    why t1 and t2 are not equal?
    when two objects are said to be equal?
  • Assam
    New Member
    • Aug 2007
    • 4

    #2
    Object.Equal method is a virtual method. So if you call the base class method it will compare the references of both objects(i.e. both objects referring to same memory location).
    You can override this method in your class to give your own functionality. One possible reason could be to compare the state of both objects as you require.
    If this is not helpful let me know i will provide the sample code.

    Comment

    Working...