C #3.0 new feature learning notes (4): Expansion Method

Source: Internet
Author: User

Extension Method.

I believe everyone has heard or used this new feature, which is one of my favorite new features.

When we need to extend the functions of an existing class, we usually think of inheritance, inherit the existing class, and then add new behaviors to it. The extension method added in C #3.0 provides another way to implement function expansion. We can extend the existing classes without using inheritance, this method does not generate new types, but adds new methods to existing classes to complete function expansion.

When extending an existing class, we need to write all the extension methods in a static class, which is equivalent to storing the container of the extension method, all extension methods can be written here. In addition, the extension method adopts a new declaration method:

Public static return type Extension Method Name (this type of extension sourceObj [, extension method parameter list])

Unlike normal method declaration, the first parameter of an extension method starts with the this keyword, followed by the extended type name, and then is the real parameter list. The following is an example:

 

Namespace JackDong. CSharp3Feature
{
Public static class MethodExtend
{
Public static bool OutLength (this string str ){
If (str. Length> 10) return true;
Return false;
}
}
}

 

If you want to call this function, you can introduce the namespace (using JackDong. CSharp3Feature;) in all the classes you call ;)

Then you can call this method in your own method: string. OutLength (yourstring );

Is it amazing.

We all know that Object is the ancestor of all classes. What if we extend the Object method? You can call your extended methods in all classes. I will not repeat them here. If you don't believe it, you can give it a try.

Another example:

 

Namespace JackDong. CSharp3Feature
{
Static class Extensions
{
Public static int ToInt32 (this string source)
{
Return Int32.Parse (source );
}

Public static T [] Slice <T> (this T [] source, int index, int count)
{
If (index <0 | count <0 | index + count> source. Length)
{
Throw new ArgumentException ();
}

T [] result = new T [count];
Array. Copy (source, index, result, 0, count );
Return result;
}
}

Class ExtensionMethods: AppRunner. AbstractApplication
{
Public override void Run ()
{
String number = "123 ";
Console. WriteLine (number. ToInt32 ());

Int [] intArray = new int [] {1, 2, 3 };

IntArray = intArray. Slice (1, 2 );

Foreach (var I in intArray)
Console. WriteLine (I );
}
}
}

 

In the preceding example, there are two extension methods in the static Extensions class. The first method is the extension of the string class. It adds the ToInt32 Method to the string class, and this method has no parameter, returns an int type value, which converts numeric characters to integers. With this extension method, you can call the ToInt32 Method for any string class object, as defined in the method itself.

The second extension method is a generic method, which is an extension of all array types. This method completes the array slicing operation.

The Linq expression in C #3.0 uses a large number of extension methods to query data.

 

Related Article

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.