GetHashCode on null fields?

前端 未结 2 1730
离开以前
离开以前 2021-02-07 03:30

How do I deal with null fields in GetHashCode function?

Module Module1
  Sub Main()
    Dim c As New Contact
         


        
2条回答
  •  广开言路
    2021-02-07 04:18

    Typically, you check for null and use 0 for that "part" of the hash code if the field is null:

    return (Name == null ? 0 : Name.GetHashCode()) ^ 
      (Address == null ? 0 : Address.GetHashCode());
    

    (pardon the C#-ism, not sure of the null check equivalent in VB)

提交回复
热议问题