question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Easy HashCode and Equals override

See original GitHub issue

Proposition

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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
CyrusNajmabadicommented, Dec 12, 2018

I wonder what are those more important things.

The current set of 8.0 features for example 😃

I believe these cases better show the importance of the community feedback

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.

1reaction
gaftercommented, Dec 9, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found