Go Explanation of extension methods in C #

Source: Internet
Author: User

Explanation of extension methods in C #

The purpose of an extension method is to add a method to an existing type, which can be either a data type such as int,string or a custom data type.

An understanding of how to add a method to a data type: Generally speaking, the int data type has a ToString method, which is to convert the int data to the type of the string, for example, now we want to add a bit of something when converting to a string, such as adding a character a. Then the previous ToString is not good, because it is just it our int data is converted to a string type, but it is not possible to add a letter A. So this is going to use the so-called extension method.

First, let's look at a way to add an extension to an existing type.

We want to add an Add method to the string type, which is the function of adding a letter A to the strings.

You must be a static class to add the extension method static class program       {        static void Main (string[] args)        {            string str = "quzijing";// Note To call the extension method, you must use the object to call             string newstr = str. Add ();            Console.WriteLine (NEWSTR);            Console.readkey ();        } The declaration extension method        //extension method must be static, add has three parameters        //this must have, string represents the type I want to extend, Stringname represents the object name        //Three parameter this and the type of the extension is necessary, Object names can be arbitrarily taken if you need to pass parameters,//Add a variable to public        static  string  Add (This string stringname)        {            return Stringname+ "a";        }}

Let's try adding an extension method to our custom type and adding a passed parameter.

First we declare a student class, which contains two methods Stuinfo,getstuinfo. The instance code is as follows:

public class Student    {public        string stuinfo ()        {            return "student basic information";        }        Public  string Getstuinfo (String stuname, String stunum)        {       return string. Format ("Student info: \ n" + "name: {0} \ n" + "study number: {1}", Stuname, Stunum);}     }

After that we declare a static class named Extensionstudentinfo, which must be static

The function of this class is to include some methods that we want to extend, where we declare two extension methods of the student type, and the student type is our custom type. The sample code is as follows:

public static class Extensionstudentinfo    {        //Declaration extension Method        //The method to be extended must be a static method, add has three parameters        //this must have, String represents the type I want to extend, Stringname represents the object name        //Three parameter this and the extended type is necessary, the object name can be arbitrarily taken if you need to pass parameters, add a variable to public        static string Extensionstuinfo (This Student stuname)        {            return stuname.stuinfo ();        }        Declaration extension Method        //The method to be extended must be a static method, add has three parameters        //this must have, string represents the type I want to extend, Stringname represents the object name        // Three parameters this and the type of extension are necessary, the object name can be arbitrarily taken if you need to pass parameters, where we added two string type parameter public        static string Extensiongetstuinfo (this Student Student, String stuname, String stunum)        {            return student.getstuinfo (Stuname, stunum) + "\ n read finished";        }    }

After the above work is done, we can use our extension method, and note that the extension method must be called with the object.

static void Main (string[] args)        {            Student newstudent = new Student ();//To invoke our extension method using an object            string stuinfo = Newstudent. Extensionstuinfo ();            Console.WriteLine (stuinfo);//To use an object to call our extension method string stuinformation = Newstudent. Extensiongetstuinfo ("quzijing", "20081766");            Console.WriteLine (stuinformation);            Console.readkey ();        }

Go Explanation of extension methods in C #

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.