Microsoft.Bcl.HashCode 6.0.0

About

Provides the HashCode type for .NET Standard 2.0. This package is not required starting with .NET Standard 2.1 and .NET Core 3.0.

The HashCode type is shipped as part of the shared framework starting with .NET 5.

Key Features

The HashCode type offered in this assembly combines the hash code for multiple values into a single hash code.

How to Use

The static methods in this class combine the default hash codes of up to eight values.

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.

The instance methods in this class combine the hash codes of more than eight values.

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.

The instance methods also combine the hash codes produced by a specific IEqualityComparer implementation.

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.

The HashCode structure must be passed by-reference to other methods, as it is a value type.

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.

Main Types

The main types provided by this library are:

  • System.HashCode

Additional Documentation

License

Microsoft.Bcl.HashCode is released as open source under the MIT license.

Showing the top 20 packages that depend on Microsoft.Bcl.HashCode.

Packages Downloads
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
12
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
13
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
14
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
15
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
16
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
17
CommunityToolkit.HighPerformance
This package includes high performance .NET helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
300
CommunityToolkit.HighPerformance
This package includes high performance .NET Standard helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
12
CommunityToolkit.HighPerformance
This package includes high performance .NET Standard helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
13
CommunityToolkit.HighPerformance
This package includes high performance .NET Standard helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
14
CommunityToolkit.HighPerformance
This package includes high performance .NET Standard helpers such as: - Memory2D<T> and Span2D<T>: two types providing fast and allocation-free abstraction over 2D memory areas. - ArrayPoolBufferWriter<T>: an IBufferWriter<T> implementation using pooled arrays, which also supports IMemoryOwner<T>. - MemoryBufferWriter<T>: an IBufferWriter<T>: implementation that can wrap external Memory<T>: instances. - MemoryOwner<T>: an IMemoryOwner<T> implementation with an embedded length and a fast Span<T> accessor. - SpanOwner<T>: a stack-only type with the ability to rent a buffer of a specified length and getting a Span<T> from it. - StringPool: a configurable pool for string instances that be used to minimize allocations when creating multiple strings from char buffers. - String, array, Memory<T>, Span<T> extensions and more, all focused on high performance. - HashCode<T>: a SIMD-enabled extension of HashCode to quickly process sequences of values. - BitHelper: a class with helper methods to perform bit operations on numeric types. - ParallelHelper: helpers to work with parallel code in a highly optimized manner. - Box<T>: a type mapping boxed value types and exposing some utility and high performance methods. - Ref<T>: a stack-only struct that can store a reference to a value of a specified type. - NullableRef<T>: a stack-only struct similar to Ref<T>, which also supports nullable references.
15
CsvHelper
A library for reading and writing CSV files. Extremely fast, flexible, and easy to use. Supports reading and writing of custom class objects.
21
Microsoft.EntityFrameworkCore
Entity Framework Core is a lightweight and extensible version of the popular Entity Framework data access technology. Commonly Used Types: Microsoft.EntityFrameworkCore.DbContext Microsoft.EntityFrameworkCore.DbSet
12
Microsoft.EntityFrameworkCore
Entity Framework Core is a lightweight and extensible version of the popular Entity Framework data access technology. Commonly Used Types: Microsoft.EntityFrameworkCore.DbContext Microsoft.EntityFrameworkCore.DbSet
13
Microsoft.EntityFrameworkCore
Entity Framework Core is a lightweight and extensible version of the popular Entity Framework data access technology. Commonly Used Types: Microsoft.EntityFrameworkCore.DbContext Microsoft.EntityFrameworkCore.DbSet
19
Npgsql
Npgsql is the open source .NET data provider for PostgreSQL.
13
Npgsql
Npgsql is the open source .NET data provider for PostgreSQL.
19
Npgsql
Npgsql is the open source .NET data provider for PostgreSQL.
40

.NET Framework 4.6.2

  • No dependencies.

.NET 6.0

  • No dependencies.

.NET Standard 2.0

  • No dependencies.

.NET Standard 2.1

  • No dependencies.

Version Downloads Last updated
6.0.0 5 11/14/2024
1.1.1 14,000 04/25/2022
1.1.0 13,912 04/21/2022
1.1.0-preview3.19551.4 8 11/04/2022
1.1.0-preview2.19523.17 3 11/28/2024
1.1.0-preview1.19504.10 4 12/02/2024
1.0.0 4 11/22/2024
1.0.0-rc1.19456.4 9 05/22/2024
1.0.0-preview9.19421.4 4 11/19/2024
1.0.0-preview9.19416.11 12 05/22/2024
1.0.0-preview8.19405.3 1 12/12/2024
1.0.0-preview7.19362.9 7 11/29/2024
1.0.0-preview6.19303.8 6 11/16/2024
1.0.0-preview6.19264.9 3 12/04/2024
1.0.0-preview6.19259.10 3 12/05/2024