473,385 Members | 1,872 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,385 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 1540
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.