[AccessD] CodePlex WCF Community code snippet

Salakhetdinov Shamil mcp2004 at mail.ru
Tue May 1 13:26:41 CDT 2012


Hi Gustav --

Yes, CompareObjects6

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);
}

beats even

public static bool CompareObjects8<T>(T o1, T o2) where T : class
{
    // Gustav's version refactored 2
    if (o1 == null || o2 == null) return (o1 == o2);
    return o1.Equals(o2);
}

for 'Optimize code' compilation version. It's funny that when you look at decompiled code you'll find that CompareObjects6  (decompiled) look like CompareObjects8 (original) and vice versa:

public static bool CompareObjects6<T>(T o1, T o2)
{
    if (o1 == null || o2 == null)
    {
        return o1 == o2;
    }
    else
    {
        return o1.Equals(o2);
    }
}


public static bool CompareObjects8<T>(T o1, T o2)
{
    if (o1 != null && o2 != null)
    {
        return o1.Equals(o2);
    }
    else
    {
        return o1 == o2;
    }
}

When 'Optimize code' option is not used then decompiled versions of CompareObjects6 and CompareObjects8 are:

public static bool CompareObjects6<T>(T o1, T o2)
{
    bool flag;
    bool flag1;
    if (o1 == null)
    {
        flag1 = true;
    }
    else
    {
        flag1 = o2 == null;
    }
    bool flag2 = flag1;
    if (flag2)
    {
        flag = o1 == o2;
    }
    else
    {
        flag = o1.Equals(o2);
    }
    return flag;
}

public static bool CompareObjects8<T>(T o1, T o2)
{
    bool flag;
    bool flag1;
    if (o1 == null)
    {
        flag1 = false;
    }
    else
    {
        flag1 = o2 != null;
    }
    bool flag2 = flag1;
    if (flag2)
    {
        flag = o1.Equals(o2);
    }
    else
    {
        flag = o1 == o2;
    }
    return flag;
}

Thank you.

-- Shamil


Tue, 01 May 2012 17:55:05 +0200 от "Gustav Brock" <Gustav at cactus.dk>:
> 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);
> }
> 

<<< snip >>>



More information about the AccessD mailing list