Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between mutable and immutable anonymous types in VB.NET

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about the difference between mutable and immutable anonymous types in VB.NET. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

VB.NET anonymous types can be divided into two types, one is mutable anonymous type, the other is immutable anonymous type. For these two anonymous types, the rules for implementation and use are also slightly different.

Immutable VB.NET anonymous types

The immutable anonymous types in C # and VB.NET are exactly the same. These anonymous types are read-only collections of properties and contain overloads of Equals, ToString, GetHashCode, and so on. As you can see from the decompiled code, the implementation of the two is slightly different, but the result is the same: the Equals and GetHashCode methods are calculated based on all the fields in the type.

C # only allows us to use immutable anonymous types. To get an immutable anonymous type in VB.NET, we need to use the Select clause or precede each field of the type with the Key keyword, for example:

Var a = new {Name = "Tom", Age = 25}; […] Select c.Name, c.Age […] Select New With {Key c.Name, Key c.Age} Dim a = New With {Key .Name = "Tom", Key .Age = 25}

Mutable VB.NET anonymous type

Mutable anonymous types in VB.NET are much simpler than immutable anonymous types. These types don't have Equals or GetHashCode methods, so if we want to compare two objects, we have to do it manually. Mutable anonymous types also do not automatically generate the Setter of properties for the field.

For example:

[.] Select New With {c.Name, c.Age} Dim b = New With {.Name = "Tom", .Age = 25}

Partially mutable VB.NET anonymous types

If some (but not all) fields of an anonymous type are marked as Key, then we create an anonymous type that is partially mutable. For this type, fields marked Key are read-only, while fields not marked Key are readable and writable.

For partially mutable anonymous types, care must be taken when using their Equals or GetHashCode methods-- only fields marked Key will be used for calculations in both methods, while other mutable fields will be ignored. This implementation helps to place variable fields in the hash table to improve the efficiency of the query.

For example:

[.] Select New With {Key c.Name, c.Age} Dim c = New With {Key. Name = "Tom", .Age = 25} Thank you for reading! This is the end of the article on "what's the difference between changeable and immutable anonymous types in VB.NET". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report