D
Doug Holland
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
*/:p_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
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
*/:p_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