Heenan, Lambert
Lambert.Heenan at chartisinsurance.com
Tue May 1 11:06:27 CDT 2012
#6 public static bool CompareObjects6<T>(T o1, T o2) where T : class { // 'Mere mortals' version refactored 2 if (o1 != null && o2 != null) return o1.Equals(o2); return (o1 == o2); } In VB land, if o1 is not null, but o2 us null then (o1 = o2) returns Null, not False. What happens in C#? Lambert -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav Brock Sent: Tuesday, May 01, 2012 11:55 AM To: accessd at databaseadvisors.com Subject: Re: [AccessD] CodePlex WCF Community code snippet Hi Shamil CompareObjects6 looks like the winner. And it is very logical to read as well. /gustav >>> mcp2004 at mail.ru 01-05-2012 17:35 >>> Hi Gustav and Lambert at all: Have a look I have added one mode 'Mere mortals' version: C# == public static bool CompareObjects6<T>(T o1, T o2) where T : class { // 'Mere mortals' version refactored 2 if (o1 != null && o2 != null) return o1.Equals(o2); return (o1 == o2); } And here are different version decompiled (compiled with 'Optimize code' option set). What version is the winner IYO? (Do you have any other code snippets for this simple function to analyze?) Decompiled ========== // 'Mere mortals' refactored 2 (decompiled) public static bool CompareObjects6<T>(T o1, T o2) { if (o1 == null || o2 == null) { return o1 == o2; } else { return o1.Equals(o2); } } // CodePlex (decompiled) public static bool CompareObjects<T>(T o1, T o2) { if (o1 == null == o2 == null) { if (o1 == null) { return true; } else { return o1.Equals(o2); } } else { return false; } } // 'Mere Mortals' original (decompiled) public static bool CompareObjects2<T>(T o1, T o2) { if (o1 != null || o2 != null) { if (o1 == null || o2 != null) { if (o1 != null || o2 == null) { return o1.Equals(o2); } else { return false; } } else { return false; } } else { return true; } } // Gustav's original & refactored (decompiled) public static bool CompareObjects3<T>(T o1, T o2) { if (o1 != null && o2 != null) { return o1.Equals(o2); } else { if (o1 != null) { return false; } else { return o2 == null; } } } // 'Mere mortals' refactored (decompiled) public static bool CompareObjects5<T>(T o1, T o2) { if (o1 != null || o2 != null) { if (o1 != null && o2 != null) { return o1.Equals(o2); } else { return false; } } else { return true; } } Thank you. -- Shamil -- AccessD mailing list AccessD at databaseadvisors.com http://databaseadvisors.com/mailman/listinfo/accessd Website: http://www.databaseadvisors.com