Automatic definition of Equals etc?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Automatic definition of Equals etc?

  1. #1
    Join Date
    Sep 2007
    Posts
    9

    Automatic definition of Equals etc?

    Hi,

    I'm in the process of learning C#. Coming from C++, I'm used to getting some methods for free from the compiler, most notably an equality operator (that tests equality of all fields) and a copy constructor.

    In C#, it seems I have to write these methods myself, except if I use structs instead of classes, where equality and copying are automatically defined. However, since everyone seems to recommend against using structs in most cases, I wonder if it is possible to get anything similar with classes. That is, can I in any way instruct the compiler to make a default Equals method and copy constructor for my class?

    Henrik

  2. #2
    Join Date
    Dec 2007
    Posts
    234

    Re: Automatic definition of Equals etc?

    The reason it "just happens" with strutcs is because they are value-based. When you set one struct equal to another, the values are copied over. With classes it's a bit different. They are reference based. So when you set one equal to another, the pointer gets copied over... now both variables point to the same memory location, and thusly the same data.

    When it comes to copying... there's two types: shallow and deep... shallow will copy references, while a deep copy will copy values themselves. Since the compiler has no way of knowing which version you could possibly want, it leaves the copy implementation up to you. Not to mention what to do when your object contains objects too... do you shallow or deep copy those? and so on...

    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  3. #3
    Join Date
    Jul 2010
    Posts
    82

    Thumbs up Re: Automatic definition of Equals etc?

    Quote Originally Posted by TechGnome View Post
    The reason it "just happens" with strutcs is because they are value-based. When you set one struct equal to another, the values are copied over. With classes it's a bit different. They are reference based. So when you set one equal to another, the pointer gets copied over... now both variables point to the same memory location, and thusly the same data.

    When it comes to copying... there's two types: shallow and deep... shallow will copy references, while a deep copy will copy values themselves. Since the compiler has no way of knowing which version you could possibly want, it leaves the copy implementation up to you. Not to mention what to do when your object contains objects too... do you shallow or deep copy those? and so on...

    -tg
    Great explanation. Thank you, TechGnome.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center