473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Object Equality

Currently I am responsible for writing the C# coding standards document for
a client and I have been doing some investigations with ildasm to establish
some additional best practices.

When using either the C# equality operator '==' or the object.Equals()
methods, which is better?

Personally I prefer the object.Equals as it prevents accidental assignement
using only a single equals, however the C# '==' is certainly less to write
(not that that in itself justifies its use).

When you compile the code and examine the resultant IL using ildasm you see
that the C# '==' operator is not simply resolved to object.Equals ... as
such this begs the question, what is the difference between:

a == b

call bool [mscorlib /* 23000001 */]System.String/* 01000003
*/::op_Equality(string, string) /* 0A000002 */

and:

string.Equals(a, b);

call bool [mscorlib/* 23000001 */]System.String/* 01000003
*/::Equals(string, string) /* 0A000003 */

Is there any performance difference between the two equality methods?

Or any other reason to prefer one over the other?

Thanks in advance

Doug Holland
Jul 21 '05 #1
1 1541
Hi Doug,

Thanks for your post. If you take a look at IL code of
String::op_Equality:bool(string,string) as shown below, you will notice
that it just calls String::Equals. So, there is no much difference between
the two equality methods, and the code "a == b" is more readable for me.

.method public hidebysig specialname static
bool op_Equality(string a,
string b) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call bool System.String::Equals(string,
string)
IL_0007: ret
} // end of method String::op_Equality

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object.cs in rotor. What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What I don't...
0
by: Bijay Kumar | last post by:
Hi Guys, I was going through the source code of Object class (Object.cs in rotor). What I found is Equals() implemented as follows: public extern virtual bool Equals(Object obj); What...
2
by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
4
by: Arjen | last post by:
Hi, I have a class with some attributes. For example class person with name as attribute. I have add multiple persons in an arraylist. Now I need a function to get/find a person by the name...
1
by: MariusI | last post by:
I have some business objects which overrides Equals to provide syntax equality instead of just reference equality. Overriding equals gives me a warning that i must override GetHashcode(). Msdn says...
6
by: Sharon | last post by:
I have two object that has been acquired using reflection as follow: string str = "123"; object param1 = dataRow; System.Type paramType = param1.GetType(); System.Reflection.MethodInfo Parse =...
14
by: serge calderara | last post by:
Dear all, What is the proper way to check if two object are equal ? I do not mean equal on Object type only but also object value's thnaks for help regards serge
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
20
by: nicolas.pourcelot | last post by:
Hi, I want to test if an object IS in a list (identity and not equality test). I can if course write something like this : test = False myobject = MyCustomClass(*args, **kw) for element in...
16
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.