|
-
Sep 29th, 2007, 06:17 AM
#1
Thread Starter
Hyperactive Member
[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 !
-
Sep 29th, 2007, 07:40 AM
#2
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
-
Sep 29th, 2007, 08:56 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|