I was wondering how exactly they generate a hashcode from boolean types in C#/.NET?
How is GetHashCode Implemented for Booleans?
Asked Answered
You can see the actual source code for .NET here, the implmentation for GetHashCode()
for a bool is
private bool m_value;
internal const int True = 1;
internal const int False = 0;
public override int GetHashCode() {
return (m_value)?True:False;
}
(And yes, it is weird that System.Boolean
contains a bool
as a member variable, when the class is compiled the CLR treats the "primitive" types Boolean
, Byte
, SByte
, Int16
, UInt16
, Int32
, UInt32
, Int64
, UInt64
, IntPtr
, UIntPtr
, Char
, Double
, and Single
special so they can do stuff like that)
© 2022 - 2024 — McMap. All rights reserved.
true
, hashcode is 1, if the value isfalse
, hashcode is 0. – Nicholasnichole