I'm scratching my head about this as I cannot understand why the following happens the way it does:
'//VB.NET
Dim product1 = New With {.Name = "paperclips", .Price = 1.29}
Dim product2 = New With {.Name = "paperclips", .Price = 1.29}
'compare product1 and product2 and you get false returned.
Dim product3 = New With {Key .Name = "paperclips", Key .Price = 1.29}
Dim product4 = New With {Key .Name = "paperclips", Key .Price = 1.29}
'compare product3 and product4 and you get true returned.
'//C#
var product5 = new {Name = "paperclips", Price = 1.29};
var product6 = new {Name = "paperclips", Price = 1.29};
//compare products 5 and 6 and you get true.
What is happening with products 1 and 2 that makes them not behave like products 5 and 6?
Anonymous types in C# are the types which do not have a name or you can say the creation of new types without defining them. It is introduced in C# 3.0. It is a temporary data type which is inferred based on the data that you insert in an object initializer.
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.
The ValueTuple types are mutable, whereas Tuple are read-only. Anonymous types can be used in expression trees, while tuples cannot.
In C#, an anonymous type is a type (class) without any name that can contain public read-only properties only. It cannot contain other members, such as fields, methods, events, etc. You create an anonymous type using the new operator with an object initializer syntax.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With