다음을 통해 공유


HashCode 구조체

정의

여러 값에 대한 해시 코드를 단일 해시 코드로 결합합니다.

public value class HashCode
public struct HashCode
type HashCode = struct
Public Structure HashCode
상속
HashCode

예제

이 클래스의 정적 메서드는 최대 8 개 값의 기본 해시 코드를 결합합니다.

using System;
using System.Collections.Generic;

public struct OrderOrderLine : IEquatable<OrderOrderLine>
{
    public int OrderId { get; }
    public int OrderLineId { get; }

    public OrderOrderLine(int orderId, int orderLineId) => (OrderId, OrderLineId) = (orderId, orderLineId);

    public override bool Equals(object obj) => obj is OrderOrderLine o && Equals(o);

    public bool Equals(OrderOrderLine other) => OrderId == other.OrderId && OrderLineId == other.OrderLineId;

    public override int GetHashCode() => HashCode.Combine(OrderId, OrderLineId);
}

class Program
{
    static void Main(string[] args)
    {
        var set = new HashSet<OrderOrderLine>
        {
            new OrderOrderLine(1, 1),
            new OrderOrderLine(1, 1),
            new OrderOrderLine(1, 2)
        };

        Console.WriteLine($"Item count: {set.Count}.");
    }
}
// The example displays the following output:
// Item count: 2.
open System
open System.Collections.Generic

[<Struct; CustomEquality; NoComparison>]
type OrderOrderLine(orderId: int, orderLineId: int) =
    member _.OrderId = orderId
    member _.OrderLineId = orderLineId

    override _.GetHashCode() = 
        HashCode.Combine(orderId, orderLineId)

    override this.Equals(obj) =
        match obj with
        | :? OrderOrderLine as o -> (this :> IEquatable<_>).Equals o
        | _ -> false

    interface IEquatable<OrderOrderLine> with
        member _.Equals(other: OrderOrderLine) = 
            orderId = other.OrderId && orderLineId = other.OrderLineId

let set =
    HashSet<OrderOrderLine> [ OrderOrderLine(1, 1); OrderOrderLine(1, 1); OrderOrderLine(1, 2) ]
printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 2.
Public Structure OrderOrderLine
    Implements IEquatable(Of OrderOrderLine)

    Public ReadOnly Property OrderId As Integer
    Public ReadOnly Property OrderLineId As Integer

    Public Sub New(ByVal orderId As Integer, ByVal orderLineId As Integer)
        Me.OrderId = orderId
        Me.OrderLineId = orderLineId
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        Return (TypeOf obj Is OrderOrderLine) AndAlso Equals(DirectCast(obj, OrderOrderLine))
    End Function

    Public Overloads Function Equals(other As OrderOrderLine) As Boolean Implements IEquatable(Of OrderOrderLine).Equals
        Return OrderId = other.OrderId AndAlso
               OrderLineId = other.OrderLineId
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return HashCode.Combine(OrderId, OrderLineId)
    End Function

End Structure

Module Program

    Sub Main(args As String())
        Dim hashSet As HashSet(Of OrderOrderLine) = New HashSet(Of OrderOrderLine)
        hashSet.Add(New OrderOrderLine(1, 1))
        hashSet.Add(New OrderOrderLine(1, 1))
        hashSet.Add(New OrderOrderLine(1, 2))
        Console.WriteLine($"Item count: {hashSet.Count}")
    End Sub

End Module
' The example displays the following output:
' Item count: 2.

중요

ToHashCode()는 의 instance HashCode한 번 이상 호출되어야 합니다.

이 클래스의 instance 메서드는 8개 이상의 값의 해시 코드를 결합합니다.

using System;
using System.Collections.Generic;

public struct Path : IEquatable<Path>
{
    public IReadOnlyList<string> Segments { get; }

    public Path(params string[] segments) => Segments = segments;

    public override bool Equals(object obj) => obj is Path o && Equals(o);

    public bool Equals(Path other)
    {
        if (ReferenceEquals(Segments, other.Segments)) return true;
        if (Segments is null || other.Segments is null) return false;
        if (Segments.Count != other.Segments.Count) return false;

        for (var i = 0; i < Segments.Count; i++)
        {
            if (!string.Equals(Segments[i], other.Segments[i]))
                return false;
        }

        return true;
    }

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

        for (var i = 0; i < Segments?.Count; i++)
            hash.Add(Segments[i]);

        return hash.ToHashCode();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var set = new HashSet<Path>
        {
            new Path("C:", "tmp", "file.txt"),
            new Path("C:", "tmp", "file.txt"),
            new Path("C:", "tmp", "file.tmp")
        };

        Console.WriteLine($"Item count: {set.Count}.");
    }
}
// The example displays the following output:
// Item count: 2.
open System
open System.Collections.Generic

[<Struct; CustomEquality; NoComparison>]
type Path([<ParamArray>]segments: string[]) = 
    member _.Segments = 
        Array.AsReadOnly segments

    override this.Equals(obj) =
        match obj with
        | :? Path as o -> (this :> IEquatable<_>).Equals(o)
        | _ -> false

    interface IEquatable<Path> with
        member this.Equals(other: Path) =
            Object.ReferenceEquals(this.Segments, other.Segments) ||
            not (isNull this.Segments) && 
            not (isNull other.Segments) &&
            this.Segments.Count = other.Segments.Count &&
            Seq.forall2 (=) this.Segments other.Segments 

    override this.GetHashCode() =
        let hash = HashCode()

        for i = 0 to this.Segments.Count - 1 do
            hash.Add this.Segments[i]
        hash.ToHashCode()

let set = 
    HashSet<Path> [
        Path("C:", "tmp", "file.txt")
        Path("C:", "tmp", "file.tmp")
        Path("C:", "tmp", "file.txt") ]
        
printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 2.
Public Structure Path
    Implements IEquatable(Of Path)

    Public ReadOnly Property Segments As IReadOnlyList(Of String)

    Public Sub New(ParamArray ByVal segments() As String)
        Me.Segments = segments
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        Return (TypeOf obj Is Path) AndAlso Equals(DirectCast(obj, Path))
    End Function

    Public Overloads Function Equals(other As Path) As Boolean Implements IEquatable(Of Path).Equals
        If ReferenceEquals(Segments, other.Segments) Then Return True
        If Segments Is Nothing OrElse other.Segments Is Nothing Then Return False
        If Segments.Count <> other.Segments.Count Then Return False

        For i As Integer = 0 To Segments.Count - 1
            If Not String.Equals(Segments(i), other.Segments(i)) Then Return False
        Next

        Return True
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim hash As HashCode = New HashCode()

        For i As Integer = 0 To Segments?.Count - 1
            hash.Add(Segments(i))
        Next

        Return hash.ToHashCode()
    End Function

End Structure

Module Program

    Sub Main(args As String())
        Dim hashSet As HashSet(Of Path) = New HashSet(Of Path) From {
            New Path("C:", "tmp", "file.txt"),
            New Path("C:", "tmp", "file.txt"),
            New Path("C:", "tmp", "file.tmp")
        }
        Console.WriteLine($"Item count: {hashSet.Count}.")
    End Sub

End Module
' The example displays the following output:
' Item count: 2.

instance 메서드는 특정 IEqualityComparer<T> 구현에서 생성된 해시 코드도 결합합니다.

using System;
using System.Collections.Generic;

public struct Path : IEquatable<Path>
{
    public IReadOnlyList<string> Segments { get; }

    public Path(params string[] segments) => Segments = segments;

    public override bool Equals(object obj) => obj is Path o && Equals(o);

    public bool Equals(Path other)
    {
        if (ReferenceEquals(Segments, other.Segments)) return true;
        if (Segments is null || other.Segments is null) return false;
        if (Segments.Count != other.Segments.Count) return false;

        for (var i = 0; i < Segments.Count; i++)
        {
            if (!string.Equals(Segments[i], other.Segments[i], StringComparison.OrdinalIgnoreCase))
                return false;
        }

        return true;
    }

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

        for (var i = 0; i < Segments?.Count; i++)
            hash.Add(Segments[i], StringComparer.OrdinalIgnoreCase);

        return hash.ToHashCode();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var set = new HashSet<Path>
        {
            new Path("C:", "tmp", "file.txt"),
            new Path("C:", "TMP", "file.txt"),
            new Path("C:", "tmp", "FILE.TXT")
        };

        Console.WriteLine($"Item count: {set.Count}.");
    }
}
// The example displays the following output:
// Item count: 1.
open System
open System.Collections.Generic

[<Struct; CustomEquality; NoComparison>]
type Path([<ParamArray>]segments: string[]) =
    member _.Segments = 
        Array.AsReadOnly segments

    override this.Equals(obj) =
        match obj with
        | :? Path as o -> (this :> IEquatable<_>).Equals(o)
        | _ -> false

    interface IEquatable<Path> with
        member this.Equals(other: Path) =
            Object.ReferenceEquals(this.Segments, other.Segments) ||
            not (isNull this.Segments) && 
            not (isNull other.Segments) &&
            this.Segments.Count = other.Segments.Count &&
            Seq.forall2 (fun x y -> String.Equals(x, y, StringComparison.OrdinalIgnoreCase)) this.Segments other.Segments 

    override this.GetHashCode() =
        let hash = HashCode()

        for i = 0 to this.Segments.Count - 1 do
            hash.Add(this.Segments[i], StringComparer.OrdinalIgnoreCase)
        hash.ToHashCode()

let set = 
    HashSet<Path> [
        Path("C:", "tmp", "file.txt")
        Path("C:", "tmp", "file.tmp")
        Path("C:", "tmp", "file.txt") ]

printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 1.
Public Structure Path
    Implements IEquatable(Of Path)

    Public ReadOnly Property Segments As IReadOnlyList(Of String)

    Public Sub New(ParamArray ByVal segments() As String)
        Me.Segments = segments
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        Return (TypeOf obj Is Path) AndAlso Equals(DirectCast(obj, Path))
    End Function

    Public Overloads Function Equals(other As Path) As Boolean Implements IEquatable(Of Path).Equals
        If ReferenceEquals(Segments, other.Segments) Then Return True
        If Segments Is Nothing OrElse other.Segments Is Nothing Then Return False
        If Segments.Count <> other.Segments.Count Then Return False

        For i As Integer = 0 To Segments.Count - 1
            If Not String.Equals(Segments(i), other.Segments(i), StringComparison.OrdinalIgnoreCase) Then Return False
        Next

        Return True
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim hash As HashCode = New HashCode()

        For i As Integer = 0 To Segments?.Count - 1
            hash.Add(Segments(i), StringComparer.OrdinalIgnoreCase)
        Next

        Return hash.ToHashCode()
    End Function
    
End Structure

Module Program

    Sub Main(args As String())
        Dim hashSet As HashSet(Of Path) = New HashSet(Of Path) From {
            New Path("C:", "tmp", "file.txt"),
            New Path("C:", "TMP", "file.txt"),
            New Path("C:", "tmp", "FILE.TXT")
        }
        Console.WriteLine($"Item count: {hashSet.Count}.")
    End Sub

End Module
' The example displays the following output:
' Item count: 1.

구조체는 HashCode 값 형식이므로 다른 메서드에 대한 참조로 전달되어야 합니다.

using System;
using System.Collections.Generic;

public struct Path : IEquatable<Path>
{
    public IReadOnlyList<string> Segments { get; }

    public Path(params string[] segments) => Segments = segments;

    public override bool Equals(object obj) => obj is Path o && Equals(o);

    public bool Equals(Path other)
    {
        if (ReferenceEquals(Segments, other.Segments)) return true;
        if (Segments is null || other.Segments is null) return false;
        if (Segments.Count != other.Segments.Count) return false;

        for (var i = 0; i < Segments.Count; i++)
        {
            if (!PlatformUtils.PathEquals(Segments[i], other.Segments[i]))
                return false;
        }

        return true;
    }

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

        for (var i = 0; i < Segments?.Count; i++)
            PlatformUtils.AddPath(ref hash, Segments[i]);

        return hash.ToHashCode();
    }
}

internal static class PlatformUtils
{
    public static bool PathEquals(string a, string b) => string.Equals(a, b, StringComparison.OrdinalIgnoreCase);
    public static void AddPath(ref HashCode hash, string path) => hash.Add(path, StringComparer.OrdinalIgnoreCase);
}

class Program
{
    static void Main(string[] args)
    {
        var set = new HashSet<Path>
        {
            new Path("C:", "tmp", "file.txt"),
            new Path("C:", "TMP", "file.txt"),
            new Path("C:", "tmp", "FILE.TXT")
        };

        Console.WriteLine($"Item count: {set.Count}.");
    }
}
// The example displays the following output:
// Item count: 1.
open System
open System.Collections.Generic

module PlatformUtils =
    let pathEquals a b = String.Equals(a, b, StringComparison.OrdinalIgnoreCase)
    let addPath (hash: byref<HashCode>) path = hash.Add(path, StringComparer.OrdinalIgnoreCase)

[<Struct; CustomEquality; NoComparison>]
type Path([<ParamArray>]segments: string[]) =
    member _.Segments = 
        Array.AsReadOnly segments

    override this.Equals(obj) =
        match obj with
        | :? Path as o -> (this :> IEquatable<_>).Equals(o)
        | _ -> false

    interface IEquatable<Path> with
        member this.Equals(other: Path) =
            Object.ReferenceEquals(this.Segments, other.Segments) ||
            not (isNull this.Segments) && 
            not (isNull other.Segments) &&
            this.Segments.Count = other.Segments.Count &&
            Seq.forall2 PlatformUtils.pathEquals this.Segments other.Segments 

    override this.GetHashCode() =
        let mutable hash = HashCode()

        for i = 0 to this.Segments.Count - 1 do
            PlatformUtils.addPath &hash this.Segments[i]
        hash.ToHashCode()


let set =
    HashSet<Path> [
        Path("C:", "tmp", "file.txt")
        Path("C:", "TMP", "file.txt")
        Path("C:", "tmp", "FILE.TXT") ]

printfn $"Item count: {set.Count}."

// The example displays the following output:
// Item count: 1.
Public Structure Path
    Implements IEquatable(Of Path)

    Public ReadOnly Property Segments As IReadOnlyList(Of String)

    Public Sub New(ParamArray ByVal segments() As String)
        Me.Segments = segments
    End Sub

    Public Overrides Function Equals(obj As Object) As Boolean
        Return (TypeOf obj Is Path) AndAlso Equals(DirectCast(obj, Path))
    End Function

    Public Overloads Function Equals(other As Path) As Boolean Implements IEquatable(Of Path).Equals
        If ReferenceEquals(Segments, other.Segments) Then Return True
        If Segments Is Nothing OrElse other.Segments Is Nothing Then Return False
        If Segments.Count <> other.Segments.Count Then Return False

        For i As Integer = 0 To Segments.Count - 1
            If Not PathEquals(Segments(i), other.Segments(i)) Then Return False
        Next

        Return True
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim hash As HashCode = New HashCode()

        For i As Integer = 0 To Segments?.Count - 1
            AddPath(hash, Segments(i))
        Next

        Return hash.ToHashCode()
    End Function
    
End Structure

Friend Module PlatformUtils

    Public Function PathEquals(ByVal a As String, ByVal b As String) As Boolean
        Return String.Equals(a, b, StringComparison.OrdinalIgnoreCase)
    End Function

    Public Sub AddPath(ByRef hash As HashCode, ByVal path As String)
        hash.Add(path, StringComparer.OrdinalIgnoreCase)
    End Sub

End Module

Module Program

    Sub Main(args As String())
        Dim hashSet As HashSet(Of Path) = New HashSet(Of Path) From {
            New Path("C:", "tmp", "file.txt"),
            New Path("C:", "TMP", "file.txt"),
            New Path("C:", "tmp", "FILE.TXT")
        }
        Console.WriteLine($"Item count: {hashSet.Count}.")
    End Sub

End Module
' The example displays the following output:
' Item count: 1.

설명

를 사용하여 HashCode 여러 값(예: 구조체 또는 클래스의 필드)을 단일 해시 코드로 결합할 수 있습니다. 이 구조에는 다르게 작동하는 정적 및 instance 메서드가 있습니다.

  • 정적 메서드는 결합할 최대 8개의 값 집합을 허용합니다.
  • 두 instance 메서드는 한 번에 하나씩 값을 수락하는 스트리밍 방식으로 작동합니다.

경고

구현이 어셈블리 버전 간에 변경 될 수 있으므로 해시 코드를 구현 세부 정보로 고려하는 것이 가장 좋습니다. 에서 생성된 HashCode 해시 코드를 직렬화된 구조체(예: 디스크 내)에 저장하지 마세요. HashCode는 정적으로 초기화된 임의 시드를 사용하여 이 모범 사례를 적용합니다. 즉, 해시 코드는 운영 체제 프로세스의 scope 내에서만 결정적입니다.

메서드

Add<T>(T)

한 개의 값을 해시 코드에 추가합니다.

Add<T>(T, IEqualityComparer<T>)

해시 코드 함수를 제공하는 형식을 지정하여 해시 코드에 한 개의 값을 추가합니다.

AddBytes(ReadOnlySpan<Byte>)

해시 코드에 바이트 범위를 추가합니다.

Combine<T1,T2,T3,T4,T5,T6,T7,T8>(T1, T2, T3, T4, T5, T6, T7, T8)

여덟 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1,T2,T3,T4,T5,T6,T7>(T1, T2, T3, T4, T5, T6, T7)

일곱 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1,T2,T3,T4,T5,T6>(T1, T2, T3, T4, T5, T6)

여섯 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1,T2,T3,T4,T5>(T1, T2, T3, T4, T5)

다섯 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1,T2,T3,T4>(T1, T2, T3, T4)

네 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1,T2,T3>(T1, T2, T3)

세 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1,T2>(T1, T2)

두 개의 값을 하나의 해시 코드로 결합합니다.

Combine<T1>(T1)

지정된 값에 의해 반환된 해시 코드를 확산시킵니다.

Equals(Object)
사용되지 않음.

이 메서드는 지원되지 않으며 호출되지 않습니다.

GetHashCode()
사용되지 않음.

이 메서드는 지원되지 않으며 호출되지 않습니다.

ToHashCode()

연속 Add 호출 후에 최종 해시 코드를 계산합니다.

적용 대상