Problems with EqulatyComparer

  • Thread starter Thread starter Jakob Lithner
  • Start date Start date
J

Jakob Lithner

I have a custom generic comparer like this:

internal class SyncItemComparer : IEqualityComparer<ISyncItem>
{
public bool Equals(ISyncItem x, ISyncItem y)
{
return x.GlobalID.Equals(y.GlobalID);
}

public int GetHashCode(ISyncItem obj)
{
return obj.GlobalID.GetHashCode();
}
}


Then i try to use it in this method:

void Sync<ISyncItem>(IEnumerable<ISyncItem> masterItems,
IEnumerable<ISyncItem> clientItems)
{
var comparer = new SyncItemComparer();

foreach (var master in masterItems)
{
if(clientItems.Contains<ISyncItem>(master, comparer))
{

}
}
}


The compiler warns on the Contains method:
Argument type 'MyApp.Business.Comparers.SyncItemComparer' is not assignable
to 'System.Collections.Generic.IEqualityComparer<ISyncItem>`

But the SyncItemComparer implements
System.Collections.Generic.IEqualityComparer<ISyncItem>!

It is very late and I must be blind to something ......
 
Good morning Jakob,

I think the implementation of the class SyncItemComparer is fine. The
problem should be at the declaration of the method Sync.
Is the method Sync a generic one? If so, we can define it as the
following:
============================================================
void Sync<T>(IEnumerable<T> masterItems, IEnumerable<T> clientItems,
IEqualityComparer<T> comparer)
{
foreach (var master in masterItems)
{
if (clientItems.Contains(master, comparer))
{

}
}
}

And calling this method like:
Sync<ISyncItem>(list1, list2, new SyncItemComparer());
============================================================

If the method Sync is a regular method, we can remove the generic
declaration part as the following:
============================================================
void Sync(IEnumerable<ISyncItem> masterItems, IEnumerable<ISyncItem>
clientItems)
{
var comparer = new SyncItemComparer();
foreach (var master in masterItems)
{
if (clientItems.Contains(master, comparer))
{

}
}
}
============================================================

Please feel free to let me know, if you have any questions.

Have a nice day!


Best Regards,
Lingzhi Sun
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Good eye.

Of course one should never ever name a generic parameter type the same as a
real type, or this confusion is sure to follow.
 
Back
Top