N
N
Nikita072021-08-16 12:02:43
Java
Nikita07, 2021-08-16 12:02:43

How to rewrite this code in C#?

Hello everyone, help me rewrite this code (Java language) into C#

@Override
        public int hashCode() {
            return Objects.hash(
                getGitCommitId(),
                getGitCommitIdDescribeShort(),
                getBuildTimeStamp(),
                getProjectVersion(),
                getCopyright(),
                getLicense(),
                getUrl(),
                getBuildJDKVersion(),
                getTargetJREVersion());
        }


Tried like this, but there is an error that GetHashCode cannot take 9 arguments
public override int GetHashCode()
            {
                return base.GetHashCode(
                    GetGitCommitId(),
                    GetGitCommitIdDescribeShort(),
                    GetBuildTimeStamp(),
                    GetProjectVersion(),
                    GetCopyright(),
                    GetLicense(),
                    GetUrl(),
                    GetBuildJDKVersion(),
                    GetTargetJREVersion());
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2021-08-16
@Nikita07

Use the Add method from the HashCode structure :

public override int GetHashCode()
{
    var hashCode = new HashCode();

    hashCode.Add(GetGitCommitId());
    hashCode.Add(GetGitCommitIdDescribeShort());
    hashCode.Add(GetBuildTimeStamp());
    hashCode.Add(GetProjectVersion());
    hashCode.Add(GetCopyright());
    hashCode.Add(GetLicense());
    hashCode.Add(GetUrl());
    hashCode.Add(GetBuildJDKVersion());
    hashCode.Add(GetTargetJREVersion());

    return hashCode.ToHashCode();
}

If you had less than 9 arguments, you could use the Combine method . But in C#, it's better to use properties
instead of get methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question