Skip to content

GetHashCode seems to only use the first field of a struct to calculate hash code #28801

@miguep

Description

@miguep

With a struct like this:

    struct MyStruct
        {
            public float a,b;
        };

if I try the following:

            MyStruct myStruct1 = new MyStruct { b= 1.0f, a = 2.0f};
            MyStruct myStruct2 = new MyStruct { b= 1.0f, a = 1.0f};

            int hash = myStruct1.GetHashCode();
            int hash2 = myStruct2.GetHashCode();

hash and hash2 are different, but trying this:

            MyStruct myStruct1 = new MyStruct { a= 1.0f, b = 2.0f};
            MyStruct myStruct2 = new MyStruct { a= 1.0f, b = 1.0f};

            int hash = myStruct1.GetHashCode();
            int hash2 = myStruct2.GetHashCode();

hash and hash2 are the same.

This behavior is different from the one in .NET Framework

Metadata

Metadata

Assignees

Labels

area-System.RuntimequestionAnswer questions and provide assistance, not an issue with source code or documentation.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions