. Net, have you forgotten? (8) -- from dynamic to feature misuse

Source: Internet
Author: User

1. Summary

EachProgramMembers want to write beautifulCodeBut what is beautiful? I think everyone has their own opinions. Let me talk about several typical ideas:

A. write code that others cannot understand, which makes others feel very advanced.

B. Write a short code.

C. write code with the latest Language Features

I don't comment on this. After all, everyone has his own point of view, and I cannot prove that I am right. But here I want to talk about some typical misuse.

2. Start with dynamic

As one of C #4.0 updates, dynamic has been pushed to the forefront of many technical forums. I have read a lot about dynamic, but I still keep my point of view. Since we use Microsoft, while we are using a language feature, we must first find out why Microsoft wants to launch this language and do not use it blindly. This is often counterproductive.

Next I will look at a traditional code in most Tutorials:

 Namespace Leleapplication1
Class Program
Static void Main (String [] ARGs)
Dynamic P = New People ( "Test" );
Console . Writeline (P. getname ());
Class People
Private string Name;
Public String Getname ()
Return Name;
Public People ( String Name)
This . Name = Name;

However, as many comments have questioned, what is the role of such an operation? (I do not doubt that many authors do not understand this, but that this will mislead many people.) So many people will follow suit and use dynamic in disorder.

Here we will explain why dynamic.

3. Dynamic and VaR

Before talking about these two keywords, we must first understand the two concepts. What is a strong language or a weak language.

I want to explain the differences among them in a classic sentence: static typing when possible and dynamic typing when needed.

In fact, the static language determines the type during compilation, while the weak type determines the type during runtime.

A simple example shows their differences:

First, the use of VaR:

Next is dynamic:

4. Why dynamic

In a foreign blog, I remember the saying that dynamic will subvert the traditional C # programming method. Once upon a time, everything in the world is an object, now all objects in the world are dynamic.

Private dynamicName;
PublicPeople (DynamicName)
This. Name = Name;
Public dynamicIntroduce ()
DynamicS ="Hello, I am"+ Name;
Public Delegate dynamicNotify(DynamicArgument );

However, as far as I personally do not agree with this statement, there are already many "in-depth analysis" dynamic "and so on.ArticleNow, I will not write more. In short, dynamic will have a great impact on efficiency. If dynamic is abused as follows:

A. It has a great impact on program efficiency.

B. Visual Studio's powerful smart sensing functions have been completely deprecated.

In this case, why should we use dynamic? In my understanding:

A. language interoperability. For example, to call a COM component of C ++, we can replace the slightly complex syntax of reflection with dynamic.

B. we all know that VaR can only be used for variables, but not for attributes. We often use VaR because we are not easy to determine the type of a variable (or attribute). Similarly, it is very likely that the type of a class attribute or method return is difficult to determine the return type. At this time, we can use dynamic. For example:

Public dynamicGetallgrilfriendsdetails ()
VaRGfdetails =FromOInDB. People
WhereO. Name =This. Name
Select New
Name = O. firstname + O. lastname,
Age = O. Age,
Address = O. Address

Why do I use dynamic gfdetails inside the method? If you hate to look at the details of the Il code, we only look at the number of decompiled C # code generated by dynamic, which may scare you:

5. Continue from misuse

Everything is always one-sidedness. Similarly, the emergence of any new things always has advantages and disadvantages. Whether it is advantage or disadvantage is not because of itself, but because the use of the environment around him is both advantage and disadvantage or advantage or disadvantage.

Any new C # language feature is also true. The environment around him is our programmer.

I have seen too many misuse, such as misuse of generics, misuse of delegation, and misuse of extension methods.

Here we will talk about the misuse of the extension method.

6. When to extend the Method

I mentioned the disadvantages of prototype in <JavaScript prototype (ii)>. Here I only want to say that adding attributes and Methods dynamically increases flexibility. However, we discuss a situation where 100 people develop a javascript project at the same time. Many inexperienced people fall in love with prototype. Is it still Object-oriented for a person to add a method to this class?

The extension method is also the same. When 100 developers develop a project at the same time and everyone writes an extension method, what will happen to this project.

When should I use the extension method? I personally think there are only three situations:

A. You are responsible for writing a component independently, and this component needs to call classes in other components. You often need to use a class and provide a "method ". At this time, you can place a special class in your component to accommodate the extension method you need.

B. A component is encapsulated by a team, but a method is not provided by this component. It is really troublesome to rewrite the component. Extension Method.

C. In fact, this is somewhat similar to the second point. When you are facing the class library provided in. NET Framework, there is no way to extend the method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.