.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 25th, 2013, 03:07 AM
deepak.sahu deepak.sahu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 deepak.sahu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 45 sec
Reputation Power: 0
Eauality Comparison of two Objects in C#.

This is a Fundamental type question, forgive me for being so amateurish.
Case 1:
Code:
Employee _emp1 = new Employee();
Employee _emp2 = _emp1;
_emp1.Equals(_emp2) ==> RETURNS a True !!
Case2:
Code:
Employee _emp1 = new Employee();
Employee _emp2 = new Employee();
_emp1.Equals(_emp2) ==> RETURNS a False !!

Could you explain me the above Comparison method and reason interms of Memory allocation perspective ?

Reply With Quote
  #2  
Old January 25th, 2013, 09:46 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 56 m 20 sec
Reputation Power: 1736
Regarding memory allocation, do you understand what exactly new does?
If not, I suggest you read about the function malloc.

I expect you will find a lot of resource on google explaining the difference and similarity of these two "functions".

Comparing object is comparing the objects location, unless for Primitive data type.

Reply With Quote
  #3  
Old January 29th, 2013, 10:50 PM
f'lar's Avatar
f'lar f'lar is offline
ASP.Net MVP
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 4,378 f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 4 m 57 sec
Reputation Power: 1509
Send a message via Google Talk to f'lar
If your type does not overload the Equals() method, you inherit one from the base Object type. The documentation on this method states that, for reference types, it checks for reference equality. This means that it checks whether two variables refer to the same object (the same location in memory).

Looking at your first sample, your _emp1 and _emp2 variable both refer to the same object. The assignment on the 2nd line did not create a copy, but assigned _emp2 to point to the object created for _emp1. Therefore, when you call the .Equals() method, it sees they are the same object and returns true.

Looking at your second sample, your _emp1 and _emp2 variables refer to different objects. The second line of code creates a completely new object for _emp2 to refer to. When you call the .Equals() method, it sees that they are different objects and so returns false.

You can choose to overload the default implementation of .Equals(), and instead look for value equality. Value equality can equate two different objects, if those objects can be said to have the same value. The most common way to do this is to also overload the GetHashCode() method, and simply compare the results of that method for each object inside your .Equals() implementation. In fact, while it's not very clearly documented that way, this is almost required for a class of any substance. It's very easy to make subtle mistakes when overloading this method, but the the current documentation on the GetHashCode() function has a section titled "Notes to Implementors" that is very helpful in avoiding them.
__________________
Primary Forum: .Net Development
Holy cow, I'm now an ASP.Net MVP!

[Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

http://twitter.com/jcoehoorn

Last edited by f'lar : January 29th, 2013 at 11:13 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Eauality Comparison of two Objects in C#.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap