What is an extension method

Source: Internet
Author: User

MSDN defines extension methods as follows: "extension methods are defined as static methods, but they are invoked through the instance method syntax. Their first argument specifies which type the method acts on, and the parameter is prefixed with the This modifier. ”

Create a new class named EString.cs in the class library

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacew.common{ Public Static classestring {/// <summary>        ///Convert a string to an int/// </summary>        /// <param name= "T" ></param>        /// <returns>returns 0 when conversion fails</returns>         Public Static intToInt ( This stringt) {intID; int. TryParse (T, outID);//here, when the conversion fails, the ID returned is 0.            returnID; }    }}

There is a toint static method in Estring, he receives a parameter of this, the type string,this string must be at the top of the method parameter, it conforms to the definition criteria, so toint is an extension method.

Referring to the namespace of the extension method can be called directly

        /// <summary>        /// using extension methods         /// </summary>        Private void intexttest ()        {            string"";             int i = s.toint ();         }

Blog Original: http://www.cnblogs.com/suger/archive/2012/05/13/2498248.html

What is an extension method

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.