Easy HashCode and Equals override
See original GitHub issueProposition
An easy way to implement default HashCode and Equals method, similar to that implemented by anonymous types
Situation
If the HashCode and Equals are already implemented by the language in some situation, why I should reimplement it? I got myself, doing this more than one time:
public class SomeThing
{
public decimal Tax { get; set; }
public decimal Comission { get; set; }
// Etc...
public override int GetHashCode()
{
return new
{
Tax,
Comission,
}.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as SomeThing;
if (other == null)
return false;
return new { Tax, Comission }.Equals(new { other.Tax, other.Comission });
}
}
Why not something like this:
[DefaultHashCode]
[DefaultEquals]
public class SomeThing
{
public decimal Tax { get; set; }
public decimal Comission { get; set; }
// Etc...
}
Or maybe:
public class SomeThing
{
public decimal Tax { get; set; }
public decimal Comission { get; set; }
// Etc...
public override object GetStrucuralProperties()
{
return new { Tax, Comission };
}
}
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Why to Override equals(Object) and hashCode() method
First, find out the right bucket using hashCode(). Secondly, search the bucket for the right element using equals(). Let us consider all the ......
Read more >Why do I need to override the equals and hashCode ...
If only equals is overriden, then when you call myMap.put(first,someValue) first will hash to some bucket and when you call myMap.put(second, ...
Read more >Java equals() and hashCode() Contracts
Always override hashCode() if we override equals(); Override equals() and hashCode() for value objects; Be aware of the traps of extending ...
Read more >How to override hashcode in Java example - Tutorial
Overriding hashCode method in Java · 1) Take a prime hash e.g. 5, 7, 17 or 31 (prime number as hash, results in...
Read more >Java - How to override equals and hashCode
To compare two Java objects, we need to override both equals and hashCode (Good practice). User.java. public class User { private String name; ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The current set of 8.0 features for example 😃
There has been minimal community feedback that this is an issue that warrants a lot of attention from MS. This issue alone only has a 10 posts from a handful of people.
We prefer to avoid giving language semantics to attributes.
The proposed C# records are exact analogs to Scala’s case classes, and would appear to address the underlying use cases.