Gustav Brock
Gustav at cactus.dk
Tue May 1 09:44:24 CDT 2012
Hi Shamil
Yes that's better/simpler.
The CodePlex solution I didn't like; it does right, of course, but you must read the method twice or more to grasp how it works.
/gustav
>>> mcp2004 at mail.ru 01-05-2012 16:22 >>>
Hi Gustav,
Very good!
I'd just have refactored it a bit:
public static bool CompareObjects3<T>(T o1, T o2) where T : class
{
if (o1 == null || o2 == null)
return (o1 == null && o2 == null);
return o1.Equals(o2);
}
Thank you.
-- Shamil
Tue, 01 May 2012 15:18:57 +0200 от "Gustav Brock" <Gustav at cactus.dk>:
> Hi Shamil
>
> How about:
>
> If (o1 == null || o2 == null)
> {
> return (o1 == null && o2 == null);
> }
> else
> {
> return o1.Equals(o2);
> }
>
> /gustav
>
>
> >>> mcp2004 at mail.ru 01-05-2012 14:34 >>>
> Hi All --
>
> Sorry I'm posting C# code snippet here as my question/quick poll is more about coding style than anything else:
>
> I have occasionally got browsing through the following CodePlex WCF Community source code:
>
> http://wcf.codeplex.com/SourceControl/changeset/view/66aa503c963c#WCFJQuery%2fTest%2fMicrosoft.Runtime.Serialization.Json.FunctionalTests%2fCommon%2fUtil.cs
>
> ...
> public static bool CompareObjects<T>(T o1, T o2) where T : class
> {
> if ((o1 == null) != (o2 == null))
> {
> return false;
> }
>
> return (o1 == null) || o1.Equals(o2);
> }
> ...
>
> And I have got stuck first trying to get its logic.
>
> Would you consider the above code more professional and maintainable and effective than the following "mere mortals" code version? If Yes/No - why?
>
> public static bool CompareObjects2<T>(T o1, T o2) where T : class
> {
> if (o1 == null && o2 == null) return true;
> if (o1 != null && o2 == null) return false;
> if (o1 == null && o2 != null) return false;
> return o1.Equals(o2);
> }
>
> Thank you ;)
>
> -- Shamil