Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
System.Diagnostics.EventLogEntry and the non-equal Equals.

Just a quick heads-up in case you're stumped with this problem, or just passing by:

The System.Diagnostics.EventLogEntry class implements the Equals method to check if two entries are identical, even if they're not the same instance. However, contrary to best practices, it does NOT overload the operator==, so these two bits of code will behave differently:

EventLog myEventLog = new EventLog("Application");
EventLogEntry entry1 = myEventLog.Entries[0];
EventLogEntry entry2 = myEventLog.Entries[0];

bool correct = entry1.Equals(entry2);
bool incorrect = entry1 == entry2;

After running this code, correct will be true while incorrect will be false.

Good to know, if you're reading event logs in your code.

Published Sunday, November 25, 2007 8:12 PM by Avner Kashtan

Filed under: , ,

Comments

# re: System.Diagnostics.EventLogEntry and the non-equal Equals.@ Monday, November 26, 2007 9:31 AM

Some are just more equal than others :P

AndrewSeven

# re: System.Diagnostics.EventLogEntry and the non-equal Equals.@ Monday, November 26, 2007 3:48 PM

Just for the record,from msdn2.microsoft.com/.../7h9bszxx(vs.80).aspx:

"Most reference types, even those that implement the Equals method, should not override ==."

Brian Dukes

# re: System.Diagnostics.EventLogEntry and the non-equal Equals.@ Monday, November 26, 2007 3:50 PM

Andrew: Man, I should have used that one. It was just lying there waiting for me, and I missed it. :)

Avner Kashtan

Leave a Comment

(required) 
(required) 
(optional)
(required)