Results 1 to 3 of 3

Thread: [2.0] Equals method

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Location
    Madrid
    Posts
    325

    [2.0] Equals method

    Hi,
    Whats best? To use or not to use Equals();

    Code:
    int a = 2;
    int b = 3;
    
    
    if(a != b)
    {
    //Sth
    }
    
    or
    
    if(!a.Equals(b))
    {
    //Sth
    }
    Cheers !

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: [2.0] Equals method

    I am not sure what differences they would have at machine code level. But there really is no big deal for something so small. In the case of something so simple i would usually go for which is easiest to understand

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

    Re: [2.0] Equals method

    In the case of value types, such as integers, there is no functional difference between the two; however, the Equals method will cause one of the two to be boxed into a reference type, and is thus less efficient than using the != or == operators.

    In the case of reference types, the != and == operators perform an identity comparison (i.e. whether or not the references point to the same object). The Equals method attempts to perform a value comparison (i.e. whether or not the references point to objects that both represent the same data [and may or may not be the same object]).
    Classes usually provide their own implementation of the Equals method in order to facilitate correct value comparisons between instances of their type.

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