If i want to compare objects and they implement the IEquatable<> interface, i have a few questions:
Why do i have to override Equals(object ) if i have to implements Equals<>
can i use == and != once i implement IEquatable ?
...
Can we do something similar to List.Contains(myItem) in order to check if a property on an item in the list equals a property on myItem.
(We have considered Contains and Exists, something like:
if (orderLines.Contains(myLine)) { ... }
but cannot get a simple expression.)
We would like something as simple as the following:
if (ord...
Background
I have two objects which have bidirectional association between them in a C# project I am working on. I need to be able to check for value equality (vs reference equality) for a number of reasons (e.g to use them in collections) and so I am implementing IEquatable and the related functions.
Assumptions
I am using C# 3.0, ....
I've read in several articles that
for reference types using IEquatable reduces the use of casting
Can someone kindly provide a convincing example.
Thanks
Clarry
...
I have an Address class in C# that looks like this:
public class Address
{
public string StreetAddress { get; set; }
public string RuralRoute { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}...
Everytime I write some data class, I usually spend so much time writing the IEquatable implementation.
The last class I wrote was something like:
public class Polygon
{
public Point[] Vertices { get; set; }
}
Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the revers...
I have implemented the IEquatable interface in a class with the following code.
public bool Equals(ClauseBE other)
{
if (this._id == other._id)
{
return true;
}
return false;
}
public override bool Equals(Object obj)
{
if...
Basically, I have the following so far:
class Foo {
public override bool Equals(object obj)
{
Foo d = obj as Foo ;
if (d == null)
return false;
return this.Equals(d);
}
#region IEquatable<Foo> Members
public bool Equals(Foo other)
{
if (this.Guid != String.Empty && t...
In .NET you need that Equals(object) and GetHashCode() are compatible. But sometimes you can't:
public class GreaterThan32Bits
{
public int X { get; set; }
public int Y { get; set; }
}
Because the data density is greater than 32 bits, and GetHashCode returns an Int32, you will have 3 solutions (assuming a correctly implemented...
When I have to implement equality comparers for
public class SampleClass
{
public int Prop { get; set; }
}
Should I make
null == new SampleClass()
and
new SampleClass() == null
and
new SampleClass().Equals(null)
false?
And what about
new SampleClass() != null
Should it be also false?
UPDATE People are questioning th...
Hello, I'm having troubles with the Except() method.
Instead returning the difference, it returns the original set.
I've tried by implementing the IEquatable and IEqualityComparer in the Account.
I've also tried creating a seperate IEqualityComparer class for Account.
When the Except() method is called from main, it doesn't seem to ca...
Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete. I want to find or write a definitive reference which must include:
How to implement IEqu...
class Program
{
static void Main(string[] args)
{
List<Book> books = new List<Book>
{
new Book
{
Name="C# in depth",
Authors = new List<Author>
{
new Author
{
FirstName = "Jon"...
I just have a simple Interface definition in my project, which I haven't even used yet. But when I try to build the project I get this error:
Access is denied: 'System.IEquatable`1[Reactor.IOptions]'.
Below is the interface:
Interface IOptions
Inherits Xml.Serialization.IXmlSerializable ' Optoins Should...
I'm trying to do a Linq GroupBy on some objects using an explicit key type. I'm not passing an IEqualityComparer to the GroupBy, so according to the docs:
The default equality comparer Default is used to compare keys.
It explains the EqualityComparer<T>.Default property like this:
The Default property checks whether
type T i...
Anyone have any opinions on whether or not IEquatable or IComparable should generally require that T is sealed (if it's a class)?
This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of immutable classes. Part of the functionality which the base class is intended to provide is autom...
Hi,
I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all inherit from interface D; they can all be stored together in a dictionary Dictionary, t...
I'm new to C#. Perhaps I'm not implementing IEquatable properly, because objects of my type that should be considered the same are not.
The class:
class CompPoint : IComparable {
public int X;
public int Y;
public CompPoint(int X, int Y) {
this.X = X;
this.Y = Y;
}
public ove...
So, the following lambda expression is not returning any elements in the collection, even though while stepping through I was able to verify that 1 item matches the criteria. I've added a sample of the class with it's IEquatable implementation.
...within a method, foo is a method parameter
var singleFoo = _barCollection.SingleOrDefault(...
Hi folks,
both the interfaces seem to compare objects for equality, so what's the major differences between them?
TIA
...