Struct System.Windows.Size has non-optimal GetHashCode implementation
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: all until at least 3.1
* Windows version: n/a
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)? no
**Problem description:**
struct ```System.Windows.Size``` uses non-effective GetHashCode function - it uses xor without shifting, recommended at the [official guidelines](https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netcore-3.1).
**Actual behavior:**
Hash code of all square sizes are equal 0. E.g. ```Size(X, X).GetHashCode() == 0``` for each X.
Code: https://github.com/dotnet/wpf/blob/ae1790531c3b993b56eba8b1f0dd395a3ed7de75/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Generated/Size.cs#L148
**Expected behavior:**
Hash Code should be computed with shifting/multiplication, for example:
```C#
public override int GetHashCode()
{
if (IsEmpty)
{
return 0;
}
else
{
return (Width.GetHashCode() * 397) ^ Height.GetHashCode();
}
}
```
**Minimal repro:**
The following code has ```O(N^2)``` complexity, because of collisions:
```C#
var set = new HashSet();
foreach(var x in Enumerable.Range(0, 100_000) {
set.Add(new Size(x, x)); // O(N) average complexity
}
```
Contributor guide
Assessment
This issue has not been assessed yet.