An anonymous type is it right? Strong type?

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

At the meeting because of some need to mention anonymous types, which also launched the two batch of people, I and another that anonymous types are not strong type, and the other two thought that an anonymous type belongs to the strong type, this blog is mainly in-depth understanding of the anonymous type.

What is an anonymous type?

Originally did not want to write the title related content, because a similar article or a lot, but for some people to read the articles by the way, is not too deep things involved here, just a simple introduction.

Anonymous types and the VaR keyword is a new feature of Visual C# 3 provides or is syntactic sugar, VaR is implicit type and not typejavascriptIn the VaR.

var user = new { Id = 1, Name = "Zhang San", Age = 15 };
Console.WriteLine(user.Name);

Detailed anonymous types

An anonymous type does not belong to the.NET Framework

image

In MSDN documentation is an anonymous type does not belong to the.NET Framework, it belongs to the Visual Studio more accurate to say that it belongs to the Visual C# 3, not too much off topic, then down remember "on.NET Framework and C#" ha ha.

Intelligent perception of an anonymous type

imageimage

From the side view of anonymous types can be intelligent perception, that whether an anonymous type is a strong type.?

An anonymous type is read-only

image

All of the properties for anonymous types in the initialization time is read-only, so once the initialization is successful it will not assign.

Unable to add two kinds of anonymous types of different attributes in an array.

image

We'll have a look to decompile after they will produce what?

image

Decompilation will find a <> f__AnonymousType0<int, string, int>, this type is what? Define the structure with Tuple<> very similar. If the definition of the type it is not a strong type? We follow the have a look.

image

Will find that is composed of a compiler generated class, we are unable to track him, also cannot determine whether he defines this category.

Anonymous types what is it right? Strong type?

First Rao not so much bent, I will elaborate on my personal view, I thinkAn anonymous type is not a "strong type".

Of course this strong type in quotes, in factIn C# all types are strongly typed, because C# itself is a strongly typed language.

But the strong type here refers to tend to use, because of anonymous types from VAR can be converted into object type, then it will not have intelligence magic, coupled with the use of VaR is limited, VAR can't be defined globally, only in the body of a method definition, soFrom the body of a method it will no longer have the intelligent sensing ring.

In fact, few, using anonymous types in the daily development of my little, prefer the anonymous types used in data binding, such as the IsEnable project to the anonymous types in the bool type new {IsEnable = isEnable? "Is": "not"} for friendly data binding, but that was only this binding places very few. For example: the drop-down box binding.

Conclusion

The second article in the blog Park, typesetting, polish and the deficiency, hope to be able to continue blogging to compensate for these problems.

Welcome to the harmonious discussion( ̄ˇ ̄)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Leo at November 16, 2013 - 11:40 PM