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 function of C # compiler

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "what is the function of the C # compiler". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

According to Marek Safar, by using a technique called type inference (type inference), the Mono C # compiler can now support implicitly typed local variables and implicitly typed arrays.

In "C-like" languages, such as C #, we often use slightly redundant methods such as "type variable = new type" to create an object. Such a declaration method will be even more tedious if the type name of the variable is long or changes frequently.

With the introduction of the "var" keyword, C # 3.0 greatly reduces this type of redundancy. Through this declaration, developers can enjoy the convenience of dynamically creating types without sacrificing the original static type support. The compiler will determine the actual type of the variable through the type information to the right of the equal sign.

It is important to note that C # is still early bound and statically typed. Some common problems in delayed binding (late binding) languages such as Visual Basic, such as "missing method exception" caused by spelling mistakes, do not appear in C #.

While it looks good, type inference is not just about improving the developer's input speed a little bit. Type inference is a necessary prerequisite for implementing anonymous classes, which are widely used in LINQ. Because anonymous classes do not have a specified type name, we cannot create an instance of that type in C# without support for type inference. (VB does this by delaying binding, but it also brings problems such as "missing method exception". )

Two types of type inference are supported in C #: implicit type variables and implicit type arrays. The implementation basis for both is exactly the same, that is, replacing "var" with the correct variable or array type expression obtained by parsing at compile time.

If the declaration and assignment of variables are not written in the same line, the compiler will not allow us to use type inference. Although it is not difficult to implement this feature from a technical point of view, the development team of the Mono C # compiler may be trying to avoid the complexity it brings, so they do not consider supporting this feature.

Marek Safar also mentions two scenarios where type inference cannot be applied.

The name implies that "implicitly typed local variables" cannot be used in the declaration of domain variables or constants, otherwise it will result in compilation errors.

I'm not sure why I set such a restriction, maybe I haven't thought it through in some places.

Note: from a technical point of view, anonymous classes also have a type name that is automatically generated by the Mono C # compiler. However, the name of the anonymous class is unpredictable, so we only need to consider its implementation details. In other words, we * treat the name of the anonymous class as if it doesn't exist at all.

This is the end of the content of "what is the function of C # Compiler"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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