The extension approach to the. NET 3.x new feature experience

Source: Internet
Author: User
Tags bool extend new features static class

Today we'll take a look at the new features of. NET 3.x: extension methods. Here's what I call the new features of the. NET 3.x, which are actually some of the new features of C # 3.0, which I call the. NET 3.x new features, given LINQ. May be so called will have some ambiguity, but I still feel so good, gossip not much to let us get to the point. I think the introduction of automation properties in c#3.0 has reduced our burden, and the extension method is my favorite, it provides a visual hint is more clever. But I also feel a bit cumbersome, when a class extension method too much, like the existence of LINQ, there will be a large number of tips ...

What is an extension method? What's his grammar like? Let's see. An extension method must be defined in a static function, and the extended method must also be a static function. This provides a good support for the extension of some method classes, especially for the interface, which is the best way to do it, usually we need a transformation when we use the DataReader object, and some methods of the DR (DataReader instance) are: GetString (int), GetInt32 (ing) ... The parameters are the indexes of the columns, and usually we need to fill in the names of the columns so that we can extend the IDataReader interface:

1  publicstaticclassExtensions
2  {
3    publicstaticstringGetString(thisIDataReaderdr,stringname)
4    {
5      returndr.GetString(dr.GetOrdinal(name));
6    }
7
8    publicstatic int GetInt32(thisIDataReaderdr,stringname)
9    {
10      returndr.GetInt32(dr.GetOrdinal(name));
11    }
12  }

From the above we can also know his grammar, the first to have a static class extensions, the extension of the IDataReader interface, you need to add this idatareader, and the parameter is the column name. Let's take a look at how smart he is in vs.

In the above we can see in the Dr Object the above extension of the hint (extension), originally IDataReader with only one getstring (int) method, now see the Overload GetString (string) Does this feel better? Note: If you want to use this extension you must introduce the namespace in which it resides.

There is an inheritance effect in the extension method, and if you are extending the method class (such as the IDataReader above), then his subclass also has this extension. If we want to have a method in all classes we can extend the object so that it will have this extension in all classes, as long as you reference the space he has made. For example: if we want to determine whether the current instance is a collection or an item in the list, we can extend the object as follows:

1 public         static bool Isemailaddress (this string text) 
 2         {
 3             regex regex = new Regex (@ "^[\w-\.] +@ ([\w-]+\.) +[\w-]{2,4}$ ");
 4 return             regex. IsMatch (text);
 5         }
 6 
 7 public         static BOOL IsIn (This object o, IEnumerable collection) 
 8         {
 9             foreach (object in collection)                 (item. Equals (o))
return                     true;
Return             false;         }

We can get this extension in all the objects and refer to the following figure:

We know that the whole of LINQ is an extension of the method, and we'll leave it until we study him later, because LINQ has three parts, one is relative to class, one is to SQL, and one is to XML. They were called linq,dlinq,xlinq. Now there seems to be no such teaching, so let's follow Microsoft's wishes.

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.