In C#, all properties of anonymous types behave as if they have the Key
modifier in VB: the properties are read-only, and they're included in equality and hash code evaluation.
In VB, properties without the Key
modifier are mutable, and are not used in the Equals
/GetHashCode
implementations.
From the Anonymous Type Definition documentation:
If an anonymous type declaration contains at least one key property, the type definition overrides three members inherited from
Object
:Equals
,GetHashCode
, andToString
. If no key properties are declared, onlyToString
is overridden. The overrides provide the following functionality:
Equals
returns True if two anonymous type instances are the same instance, or if they meet the following conditions:
- They have the same number of properties.
- The properties are declared in the same order, with the same names and the same inferred types. Name comparisons are not case-sensitive.
- At least one of the properties is a key property, and the Key keyword is applied to the same properties.
- Comparison of each corresponding pair of key properties returns True.
GetHashcode
provides an appropriately uniqueGetHashCode
algorithm. The algorithm uses only the key properties to compute the hash code.
ToString
returns a string of concatenated property values, as shown in the following example. Both key and non-key properties are included.