Skip to main content
added 57 characters in body; deleted 57 characters in body
Source Link
jason
  • 242.6k
  • 35
  • 436
  • 532

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "Z" } 

Then x == yz and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

Thus, we have shown that the only possible implementation of GetHashCode is

private readonly int constant = 17; public override int GetHashCode() { return constant; } 

All of this put together makes it clear that you need to rethink the concept you are trying model, and come up with a different definition of Equals.

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "Z" } 

Then x == y and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

Thus, we have shown that the only possible implementation of GetHashCode is

private readonly int constant = 17; public override int GetHashCode() { return constant; } 

All of this put together makes it clear that you need to rethink the concept you are trying model, and come up with a different definition of Equals.

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "Z" } 

Then x == z and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

Thus, we have shown that the only possible implementation of GetHashCode is

private readonly int constant = 17; public override int GetHashCode() { return constant; } 

All of this put together makes it clear that you need to rethink the concept you are trying model, and come up with a different definition of Equals.

added 232 characters in body; edited body
Source Link
jason
  • 242.6k
  • 35
  • 436
  • 532

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "z""Z" } 

Then x == y and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

ThereforeThus, we have shown that the only possible implementation of GetHashCode is

private readonly int constant = 17; public override int GetHashCode() { return constant; } 

All of this put together makes it clear that you need to rethink the concept you are trying model, and come up with a different definition of Equals.

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "z" } 

Then x == y and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

Therefore, you need to rethink the concept you are trying model, and come up with a different definition of Equals.

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "Z" } 

Then x == y and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

Thus, we have shown that the only possible implementation of GetHashCode is

private readonly int constant = 17; public override int GetHashCode() { return constant; } 

All of this put together makes it clear that you need to rethink the concept you are trying model, and come up with a different definition of Equals.

Source Link
jason
  • 242.6k
  • 35
  • 436
  • 532

This is a terrible definition of Equals because it is not transitive.

Consider

x = { Id = 1, Money = 0.1, Code = "X" } y = { Id = 1, Money = 0.2, Code = "Y" } z = { Id = 3, Money = 0.2, Code = "Z" } 

Then x == y and y == z but x != z.

Additionally, we can establish that the only reasonable implementation of GetHashCode is a constant map.

Suppose that x and y are distinct objects. Let z be the object

z = { Id = x.Id, Money = y.Money, Code = "z" } 

Then x == y and y == z so that x.GetHashCode() == z.GetHashCode() and y.GetHashCode() == z.GetHashCode() establishing that x.GetHashCode() == y.GetHashCode(). Since x and y were arbitrary we have established that GetHashCode is constant.

Therefore, you need to rethink the concept you are trying model, and come up with a different definition of Equals.