Results 1 to 4 of 4

Thread: Concept of Cloning and Equal operator in the real world.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2003
    Posts
    436

    Cool 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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    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
  •  



Click Here to Expand Forum to Full Width