Click here to Skip to main content
15,919,434 members
Home / Discussions / C#
   

C#

 
GeneralRe: Coding help needed Pin
bdiepeveen3-Apr-08 1:36
bdiepeveen3-Apr-08 1:36 
GeneralAbout Hashtables Pin
John.L.Ponratnam1-Apr-08 19:42
John.L.Ponratnam1-Apr-08 19:42 
GeneralRe: About Hashtables Pin
Spunky Coder1-Apr-08 19:54
Spunky Coder1-Apr-08 19:54 
GeneralRe: About Hashtables Pin
Harvey Saayman1-Apr-08 22:26
Harvey Saayman1-Apr-08 22:26 
QuestionHow to perform cut,delete operation in bitmap? Pin
Aravinthan1-Apr-08 18:51
Aravinthan1-Apr-08 18:51 
GeneralConfused with the Equals() of Object class [modified] Pin
Darmi1-Apr-08 17:48
Darmi1-Apr-08 17:48 
GeneralRe: Confused with the Equals() of Object class Pin
N a v a n e e t h1-Apr-08 20:38
N a v a n e e t h1-Apr-08 20:38 
GeneralRe: Confused with the Equals() of Object class Pin
carbon_golem2-Apr-08 2:48
carbon_golem2-Apr-08 2:48 
When you call Object.Equals(o1, o2) the static version, internally that function first checks to see if the references are not null, and then calls the virtual method o1.Equals(o2). I just checked that using Reflector so that's accurate. Now here's where it can get tricky, if you do not override the Equals method, when o1.Equals(o2) is called in the static function, it just checks object references. I'm going to repost the code that I previously posted.

namespace PersonTest {
    class Program {
        static void Main(string[] args) {
            Person p1 = new Person { Member = "ABC", OtherMember = 1 };
            Person p2 = new Person { Member = "ABC", OtherMember = 1 };
            Console.WriteLine(Object.Equals(p1, p2).ToString());
            Console.WriteLine(Object.ReferenceEquals(p1, p2).ToString());
            Console.ReadLine();
        }
    }

    public class Person {

        public String Member { get; set; }
        public Int32 OtherMember { get; set; }

        public override Boolean Equals(Object obj) {
            return obj is Person ? Equals(obj as Person) : false;
        }

        private Boolean Equals(Person obj) {
            if (Object.ReferenceEquals(this, obj)) return true;
            if ((obj == null) || (this == null)) return false;
            return obj.Member == this.Member && obj.OtherMember == this.OtherMember;
        }

        public override int GetHashCode() {
            return base.GetHashCode();
        }
    }
}


Put this into a project and the output will be True False. Then comment out both of the Equals methods in the Person class and the output will be False False. The reason is that you have 2 different references to Person classes. Regardless of the EQUALITY of the classes the IDENTITY will always be different. Object.ReferenceEquals is always IDENTITY. Object.Equals starts by checking EQUALITY, but then defaults to IDENTITY in the absence of an overridden Equals() method in your class. That's why when you comment out the Equals methods in the Person class, the Object.Equals(p1, p2) the method goes to IDENTITY and not EQUALITY. You have to supply the EQUALITY.

I hope this rather thorough explanation helps.
Scott

"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

GeneralSite Personalization - UserID from the asp_net Profile table Pin
karmasol1-Apr-08 17:46
karmasol1-Apr-08 17:46 
QuestionProblem with Singleton Pattern Pin
Zero Destiny1-Apr-08 17:39
professionalZero Destiny1-Apr-08 17:39 
GeneralRe: Problem with Singleton Pattern Pin
Simon P Stevens1-Apr-08 22:25
Simon P Stevens1-Apr-08 22:25 
GeneralRe: Problem with Singleton Pattern Pin
Rob Philpott1-Apr-08 22:49
Rob Philpott1-Apr-08 22:49 
GeneralRe: Problem with Singleton Pattern Pin
Rob Philpott1-Apr-08 22:51
Rob Philpott1-Apr-08 22:51 
GeneralRe: Problem with Singleton Pattern Pin
carbon_golem2-Apr-08 3:03
carbon_golem2-Apr-08 3:03 
GeneralRe: Problem with Singleton Pattern Pin
Zero Destiny2-Apr-08 4:22
professionalZero Destiny2-Apr-08 4:22 
QuestionHow to pass a parameter in an event Pin
Silvyster1-Apr-08 16:08
Silvyster1-Apr-08 16:08 
GeneralRe: How to pass a parameter in an event Pin
PandemoniumPasha1-Apr-08 17:38
PandemoniumPasha1-Apr-08 17:38 
GeneralRe: How to pass a parameter in an event Pin
Silvyster1-Apr-08 22:12
Silvyster1-Apr-08 22:12 
GeneralRe: How to pass a parameter in an event Pin
J4amieC1-Apr-08 22:30
J4amieC1-Apr-08 22:30 
GeneralInfos, links, best practice etc. for programming a webservice Pin
stephan_0071-Apr-08 12:02
stephan_0071-Apr-08 12:02 
GeneralRe: Infos, links, best practice etc. for programming a webservice Pin
KaptinKrunch1-Apr-08 14:05
KaptinKrunch1-Apr-08 14:05 
GeneralRe: Infos, links, best practice etc. for programming a webservice Pin
Rob Philpott1-Apr-08 22:45
Rob Philpott1-Apr-08 22:45 
Question(XML) Editing a single element within a node Pin
Stupefy1-Apr-08 10:13
Stupefy1-Apr-08 10:13 
GeneralRe: (XML) Editing a single element within a node Pin
damianrda1-Apr-08 10:56
damianrda1-Apr-08 10:56 
GeneralRe: (XML) Editing a single element within a node Pin
Stupefy1-Apr-08 11:03
Stupefy1-Apr-08 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.