Share via


Operators.hash<'T> Function (F#)

A generic hash function, designed to return equal hash values for items that are equal according to the = operator. By default it will use structural hashing for F# union, record and tuple types, hashing the complete contents of the type. The exact behavior of the function can be adjusted on a type-by-type basis by implementing GetHashCode for each type.

Namespace/Module Path: Microsoft.FSharp.Core.Operators

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
hash : 'T -> int (requires equality)

// Usage:
hash obj

Parameters

  • obj
    Type: 'T

    The input object.

Return Value

The computed hash.

Remarks

This function is named Hash in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following examples shows the use of the hash function to generate hashes for a variety of data types.

let show a = printfn "hash(%A) : %d" a (hash a) 
show 1;
show 2;
show "1"
show "2"
show "abb" 
show "aBc" // case-sensitive
show None;
show (Some 1);
show (Some 0);
show [1;2;3];
show [1;2;3;4;5;6;7;8];
show [1;2;3;4;5;6;7;8;9;10;11];
show [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15]
hash(1) : 1
hash(2) : 2
hash("1") : -842352753
hash("2") : -842352754
hash("abb") : 2103075711
hash("aBc") : 539088922
hash(<null>) : 0
hash(Some 1) : -1640531462
hash(Some 0) : -1640531463
hash([1; 2; 3]) : 1956583134
hash([1; 2; 3; 4; 5; 6; 7; 8]) : 922428386
hash([1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11]) : 1771492728
hash([1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15]) : -926589492

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Core.Operators Module (F#)

Microsoft.FSharp.Core Namespace (F#)