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 essence of Linq

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the essence of Linq". In daily operation, I believe that many people have doubts about the nature of Linq. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the essence of Linq?" Next, please follow the editor to study!

1. Anonymous type

Private static void AnonymousType () {var v = new {Name = "Zhang San", Sex = true}; / / A variable that declares a class without display and can get the context in the initializer-closure Console.WriteLine (v.Name);}

Linq essence: with anonymous types, we do not need to declare a type that is automatically generated by the C # compiler and takes advantage of the meaning of the new features of initializers and var for Linq: combined with initializers to construct new element types in the query result set.

two。 Expansion method

For example, we now want to add (extend) a method to the int type to determine whether an integer itself is even. Our expected syntax is as follows:

Private static void ExtendMethod () {int I = 2; Console.WriteLine (i.IsEven ());}

Note that there is no IsEven () method in int. To implement this method, you must write a static class and a static method:

Static class MyExtention {public static bool IsEven (this int num) / / this means that the meaning of this for int is the same as that of indexer, and int means to extend the type of int {return num% 2 = = 0;}}

Linq essence: the essence of compiling i.IsEven () is that the C # compiler generated the MyExtention.IsEven (I) code, in fact, it still does not destroy the structure of the type, and it is not really like syntax that adds an IsEven () method to int for no reason, which is different from the dynamic injection method of Visitor pattern in the design pattern.

At this point, the study on "what is the essence of Linq" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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