At a meeting, two groups of people were launched due to the need to mention the anonymous type. I and the other proposed that the anonymous type is not a strong type, the other two think that the anonymous type is strongly typed. In this blog post, we will take a closer look at the anonymous type.
What is an anonymous type?
I didn't want to write this title-related content because there are still a lot of similar articles, but I mentioned it by the way for some newcomers to better read this article. It does not involve too much content here, this is just a brief introduction.
The anonymous type and var keyword are a new feature or syntactic sugar provided by Visual C #3.0. var is an implicit type rather than a type.Javascript.
Description: anonymous type
The anonymous type does not belong to. NET Framework.
In the MSDN document, the anonymous type does not actually belong.. NET Framework, which belongs to Visual Studio. More accurately, it belongs to Visual C #3.0. If you don't talk too much about it, remember to repeat it. NET Framework and C # "Haha.
Anonymous smart sensing
From this perspective, the anonymous type can be intelligently perceived. Does it mean that the anonymous type is a strong type?
The anonymous type is read-only.
All attribute servers in the anonymous type are read-only except during initialization, so they cannot be assigned a value once the initialization is successful.
You cannot add two different attribute anonymous types to a data group.
Let's see what they will produce after decompilation?
After decompiling, you will find a <> f _ AnonymousType0 <int, string, int>. What is this type? The definition structure is very similar to that of Tuple <>. If a type is defined, isn't it a strong type? Let's track it.
The system will prompt that the class is generated by the compiler. We cannot trace it or determine whether the class is defined.
Is the anonymous type strongly typed?
First of all, let me explain my personal thoughts. I personally thinkThe anonymous type is not a "strong type".
Of course, the strong type here is enclosed by quotation marks. In factAll types in C # are strongly typed, because C # itself is a strongly typed language.
However, the strong type mentioned here is more inclined to be used, because the anonymous type is different from var and can only be converted to the object type, so it cannot have magical SMART awareness, in addition, the use of var is restricted. var cannot be defined globally and can only be defined in the method body.Without the current method body, it will no longer have the halo of intelligent sensing.
In fact, in my daily development, there are very few anonymous types, and almost none. individuals prefer to use anonymous types in data binding, for example, if the bool type IsEnable is projected to {IsEnable = isEnable? "": ""} Is used for friendly data binding, but this is limited to a very small number of such binding. For example, bind the drop-down box.
Conclusion
The second article in the blog Park is still lacking in typographical and retouching, hoping to stick to writing blog posts to make up for these problems.
Thank you for your discussion)