What's your approach on writing equality checks for the structs
and classes
you create?
1) Does the "full" equality checking require that much of boilerplate code (like override Equals
, override GetHashCode
, generic Equals
, operator==
, operator!=
)?
2) Do you specify explicitly that your classes model the IEquatable<T>
interface?
3) Do I understand correctly, that there is no actual way to automatically apply Equals
overrides, when I invoke something like a == b
and I always have to implement both the Equals
and operator==
members?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
You're right, this is a lot of boiler-plate code, and you need to implement everything separately.
I would recommend:
GetHashCode
and Equals(object)
- creating overloads for == and implementing IEquatable<T>
without doing that could result in very unexpected behaviourIEquatable<T>
if you're overriding Equals(object)
and GetHashCode
IEqualityComparer<T>
expressing the comparison you're interested in.Here's a sample implementation:
using System;
public sealed class Foo : IEquatable<Foo>
{
private readonly string name;
public string Name { get { return name; } }
private readonly int value;
public int Value { get { return value; } }
public Foo(string name, int value)
{
this.name = name;
this.value = value;
}
public override bool Equals(object other)
{
return Equals(other as Foo);
}
public override int GetHashCode()
{
int hash = 17;
hash = hash * 31 + (name == null ? 0 : name.GetHashCode());
hash = hash * 31 + value;
return hash;
}
public bool Equals(Foo other)
{
if ((object) other == null)
{
return false;
}
return name == other.name && value == other.value;
}
public static bool operator ==(Foo left, Foo right)
{
return object.Equals(left, right);
}
public static bool operator !=(Foo left, Foo right)
{
return !(left == right);
}
}
And yes, that's a heck of a lot of boilerplate, very little of which changes between implementations :(
The implementation of ==
is slightly less efficient than it might be, as it will call through to Equals(object)
which needs to do the dynamic type check... but the alternative is even more boiler-plate, like this:
public static bool operator ==(Foo left, Foo right)
{
if ((object) left == (object) right)
{
return true;
}
// "right" being null is covered in left.Equals(right)
if ((object) left == null)
{
return false;
}
return left.Equals(right);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With