Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do VB.NET anonymous types without key fields differ from C# anonymous types when compared?

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?

like image 368
BenM Avatar asked Feb 11 '14 13:02

BenM


People also ask

What is the difference between an anonymous type and a regular data type C#?

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.

Why do we need anonymous types in C#?

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.

What is the difference between tuples and anonymous types?

The ValueTuple types are mutable, whereas Tuple are read-only. Anonymous types can be used in expression trees, while tuples cannot.

What is an anonymous class in C#?

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.


1 Answers

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, and ToString. If no key properties are declared, only ToString 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 unique GetHashCode 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.

like image 57
Jon Skeet Avatar answered Sep 24 '22 00:09

Jon Skeet