|
-
May 6th, 2006, 08:14 AM
#1
Thread Starter
Hyperactive Member
Concept of Cloning and Equal operator in the real world.
Sorry for reposting the same question...
Good Morning folks,
As you all know me, being a newbiew to .NET and C# (actually OOP also), I am practicing various aspects of the OOP concepts using C# language.
This forum responded to my concept questions and helped me quite a bit recently.
Question 1: I was wondering when will in real world have to use and "Equal" operator on the reference type objects.
Question 2: when should we be using Cloning at all in the real world.
And if possible if some one can kindly give me a simple example, that would help quite a bit.
regards
nath
-
May 6th, 2006, 01:36 PM
#2
Re: Concept of Cloning and Equal operator in the real world.
1: In Java the "equals" method validates the content of each object and returns true if the content is equal, I thought it's the same in C#, but C# compares strings and any other objects/data types by overriding the '=' sign. the equal still exists for user-defined objectives. e.g. In one of your classes you might want to override the "Equals()" to check for some attribute equality (Compare IDs for data records)
2: When you say Class1 c=new Class1(); Class1 c2=c;
this means that references c and c2 point at the same object in the memory, but if you want two separate objects you use the "Clone()" method
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
May 7th, 2006, 05:13 AM
#3
Re: Concept of Cloning and Equal operator in the real world.
C# is the opposite to Java. The Equals() method is a reference comparison and the == operator can be overloaded per class for value comparison.
-
May 8th, 2006, 04:36 PM
#4
Fanatic Member
Re: Concept of Cloning and Equal operator in the real world.
Where are you people coming from? The C# implementation is not opposite of Java!
The Equals() method in C# is not a reference comparison. The == operator is the reference comparison, except when comparing strings (and of course value types) because C# automatically uses the Equals method on the string objects instead of comparing the references.
Yes, the default implementation of Equals is comparing references because an object of type Object doesn't know any other way to perform an equals comparison. You override the Equals() method when you want to provide equality based on content and not references. That's the way classes in the .NET framework do it. Don't forget to override GetHashCode() too if you override Equals().
You can override operator == if you want to, but it's not recommended unless the semantics flow naturally from the role of the class.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|