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 use of dynamic, the new feature of C # 4.0?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "what is the use of dynamic, the new feature of C# 4.0", which is easy to understand and well organized. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the use of dynamic, the new feature of C# 4.0?"

The two questions are:

◆ writes programs without IntelliSense.

◆ slows down (reflects) when running the program.

After watching New features in CSharp 4.docx today, I suddenly realized that I would tidy up my things while I was free, hoping to provide some reference to friends who do not understand the use of dynamic for the time being. Of course, due to my lack of talent and knowledge, I do not guarantee that all my views are correct. I hope you will look at this article dialectically. If there are any mistakes, please criticize and correct them.

To get to the point, the dynamic keyword is used to declare a dynamic object through which methods or read-write properties are called.

When using C # 2.0 or 3.0, if an object needs to be determined at run time and there is no information about the interface and base class, then we usually use reflection technology to call the methods or properties of this unknown object, and the dynamic provided by C # 4.0 can help us simplify these tasks. Suppose our program will get an object of uncertain type at run time, but this object must have a Print () method, and we need to call this method to print out some information, then under C # 4.0, we can use the following two lines of code to implement this requirement.

Dynamic unknowObj = GetDymanicObject (); unknowObj.Print (); / / call a method of a dynamic object

This solution is much simpler than calling the Print method with reflection, isn't it? What the programmer needs to do is not to mistype the method name Print (). VS2010 will not provide an intelligent prompt for the dynamic object, because VS does not know what the unknowObj will be at run time.

At this point, there should be a lot of friends can see from this example, when the program is compiled to unknowObj.Print (), VS will help us generate reflection code, using reflection to call Print this method, in essence, is to help us reflect automatically.

If you can understand this, it is not difficult to understand why C # wants dynamic, a monster that has no intelligence and is slow to run.

According to New features in CSharp 4, dymanic is mainly used in the following scenarios:

1. Automatic reflection

2. COM component interoperability

3. Mixed programming, such as IronRuby and IronPython

4. Deal with Html DOM objects

If there are friends who have dealt with the above work, it should not be difficult to understand.

For the specific content, please take a look at New features in CSharp 4 for yourselves, which is more detailed.

The above is all the content of the article "what is the use of dynamic, the new feature of C# 4.0?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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