Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

c# - Is there a way to automatically generate equals and hashcode method in Visual Studio

In Java when you want to have remove correctly object from a generic Collection by remove() method you have to implement equals(Object o) and remove() method which can be automatically generated in Eclipse. Example of that method looks like that ---> below.

  1. How to automatically generate that method in C# (Visual Studio, I'm on VS2013)?

  2. Maybe it is not necessary to make List.Remove() method working properly?

  3. IF it is not possible automatically how the reference Equals methods should look like? I mean how it should look like.

  4. Is Equals() method is even used in List.Remove() if so could you show me how the Equals() should be implemented to return true if we compare THE SAME OBJECTS (same address in memory)


  @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((centerPanel == null) ? 0 :          centerPanel.hashCode());
        result = prime * result + ((lowerPanel == null) ? 0 : lowerPanel.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj)
            return true;
        if(obj == null)
            return false;
        if(getClass() != obj.getClass())
            return false;
        LayoutDemo other = (LayoutDemo) obj;
        if(centerPanel == null) {
            if(other.centerPanel != null)
                return false;
        } else if(!centerPanel.equals(other.centerPanel))
            return false;
        if(lowerPanel == null) {
            if(other.lowerPanel != null)
                return false;
        } else if(!lowerPanel.equals(other.lowerPanel))
            return false;
        return true;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Lo and behold, since November 2017 even Visual Studio itself can generate meaningful implementation of these methods (at least since version 15.5.2).

Just press ctrl+. or right click inside the class and choose "Quick Actions" and then "Generate Equals and GetHashCode"

Docs for the feature: https://docs.microsoft.com/en-us/visualstudio/ide/reference/generate-equals-gethashcode-methods

    public class Foo
    {
       public Bar Bar { get; set; }
       public string FooBar { get; set; }

    public override bool Equals(object obj)
    {
        var foo = obj as Foo;
        return foo != null &&
               EqualityComparer<Bar>.Default.Equals(Bar, foo.Bar) &&
               FooBar == foo.FooBar;
    }

    public override int GetHashCode()
    {
        var hashCode = 181846194;
        hashCode = hashCode * -1521134295 + EqualityComparer<Bar>.Default.GetHashCode(Bar);
        hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FooBar);
        return hashCode;
    }

}

Update: Note tough, that you still might not want to trust VS completely and test Equals, since if your class contains Collection, Equals will depend on reference equality once more as this term is used:

EqualityComparer<IList<Foo>>.Default.Equals(SomeFoos, other.SomeFoos);

OMG anyone? And ReSharper does that too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...