Super method in C #

Source: Internet
Author: User

A long time ago, I began to pay attention to the Language Integrated Query technology. I have read a lot of articles on paper and never used them. I have been using vs for a long time. net2005 is a project. In 2005, there is a CTP version of Linq, which can only be installed in the English version. I have a Chinese version and cannot install it if I want. Now vs. net2008 has launched beat2. The release of beat2 indicates that the official version may come out at the end of the year or early next year. When this time is relatively idle, download and install it.

Vs. net2008 provides many exciting new features, one of the most prominent highlights of which is the so-called integrated language query (Linq. In some earlier versions of Linq, different functions are developed by different teams, so there are many names, such as Linq to Object, Linq to XML, and Linq to SQL, we are collectively referred to as "Linq". They provide integrated query of objects, xml integrated query, OR/M of databases and objects, and integrated query of objects respectively, in the past, most of the projects OR/M used NHibernate transplanted from Java, although this is also a very good and widely used OR/M framework, however, the use and configuration are not as simple as the use of Linq to SQL, and the degree of language integration is far less comparable. The unified integrated query technology is provided for all objects (general objects, Object Data Objects, XML data objects, and so on) in Linq. the accumulated experience and skills in object query are as follows, this function can be fully used for database queries, which means that for General C # programmers, they do not even need to study database or XML knowledge.

Why does Linq have this powerful capability? The key lies in the "Extension Method" provided by NET3.0 and later, which opens the second pulse of Ren du. Extension Methods is to add a new method to a class. You do not need to write subclass, or destroy the class encapsulation, or re-compile the class. It seems that there is nothing special about this. It's not so amazing. Let's look at a simple example.

To determine whether an object exists in a collection, write the following extension method:

Namespace MyExtensionMethods {

Public class MyMethods {

Public static bool IsIn (this object o, IEnumerable arr ){

Foreach (object e in arr ){

If (e. Equals (o )){

Return true;

}

}

Return false;

}

}

}

Note that this object o is used to declare this extension method. this indicates that this method can be added to System. the Object class is added by the compiler. To use this extension method, you only need to introduce the namespace. It's just like a's foreign aid. You don't have to worry about rewriting System. Object.

Use now:

Using MyExtensionMethods; // introduce the namespace containing this extension method

Public class Test {

Public static void Test1 () {// used for string Arrays

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.