. net extension methods

Source: Internet
Author: User

The extension method is actually adding an extension method to an existing type, which can be either an int, a string, a DateTime, or a custom type.

An extension class can be added with the premise that the extended class must be a static class.

In addition, the extension method must be a static method. So calling an extension method is called directly with the class name of the extension class.

For example, add an extension method to the String class: ToInt

public static Class Extendsstring

{

public static int ToInt (this string str)

{

int id;

Int. TryParse (t,out ID);

return ID;

}

}

As is visible from the above code, the extension method accepts a self parameter, this, type string, and this string must be placed at the first position of the method parameter.

. net extension methods

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.