How to expand in C #

Source: Internet
Author: User
Today, I read the extension method section in C # advanced programming version 6. Unfortunately, it is too general. I only have one page, which is a bit confusing, so I tried to write an extension method. Unfortunately, it was written by me! Write down the story to save time (poor memory ).
I have written two extension methods: one is for custom class extension methods, and the other is for class extension methods in the framework, which are described as follows:
1. Custom class extension methods
Custom class peoplo
1 Public   Class Peoplo
2 {
3 Public Peoplo ( Bool Sex)
4 {
5Sex=Sex;
6}
7 Public   Bool Sex {Get;Set;}
8 }


The peoplo class defines a bool type attribute sex. Sex = true indicates male, and sex = false indicates female.
The following is a static class extendpeoplo used for peoplo class extension methods, Code As follows:
1 Public   Static   Class Extendpeoplo
2 {
3 Public   Static   String Getsex ( This Peoplo PL)
4 {
5 If (Pl. Sex)
6 {
7Return "Male";
8}
9 Else
10 {
11Return "Female";
12}
13 }
14 }

Note that this class is a static class, and the extension method getsex () is also a static method. It only has one parameter peoplo pl. here we need to describe it in detail, after the keyword this, this is used to tell the compiler that this method is part of the peoplo class, and the first parameter of this method must be the keyword "this", followed by the class to be extended, in this class, getsex is extended to the peoplo class. In other words, it is necessary to extend the method for that class, the first parameter of this extension method must start with this followed by the class to be extended (this peoplo PL), and the first parameter is not displayed when the extension method is called, he just told the compiler that this extension method belongs to this class.
The following describes how to use this extension method.
The following is a default. aspx. CS created by me. The call code is as follows:   Protected   Void Page_load ( Object Sender, eventargs E)
{
Peoplo pl =   New Peoplo ( True );
Response. Write (pl. getsex ());

}

First, we create a peoplo instance PL and initialize the sex attribute of PL to true (indicating male). At this time, PL already has an extension method getsex () (The first parameter of the extension method is not displayed, so the extension method becomes a method getsex () without parameters). This is the input Pl. the code prompt function of vs2008 will display the extension method we just mentioned, for example:

The extension method is well recognized. His method name is preceded by a blue downward arrow, which indicates that the method is an extension method, using response. write (pl. getsex) output results to the leaf. The running result is: male.
This is a very simple example. I didn't come up with a better example for a while, so I had to use it first!
To put it down, I feel that there is no need for a custom class extension method. We can achieve this in a custom class. I know what I think is right, hope you can give me some advice.
Ii. Class extension methods in framework
The following example shows how to extend the getstring () method of the string class. The class of the extension method is extendstring. cs. The Code is as follows: 1 Public   Static   Class Extendstring
2 {
3 Public   Static   String Getstring ( This String str1, Int N, String Str2)
4 {
5 Int Strcount = Str1.length;
6 If (Strcount > N)
7 {
8ReturnStr1.substring (0, N)+Str2+Str1.substring (N );
9}
10 Else
11 {
12Return "Error!";
13}
14 }
15 }

The function of this method is to insert a STR string at the nth index position of str1, and then return this new string. If n> str1 is long, the string "error" is returned ", the code is simple.
The following code calls this extension method:   Protected   Void Page_load ( Object Sender, eventargs E)
{
String Str =   " I am a string " ;
Response. Write (Str. getstring ( 2 , " , " ));

}

First define a string variable STR = "I Am a string", then STR has an extension method getstring (int n, string str2). The running result is as follows: I am, a string.
I think it is more practical to use the class Extension Method in the framework. The reason is very simple. The built-in classes of the framework cannot be modified. If you do not need to use the extension method, you must implement the above functions, we can only customize one class to implement this function, and then instantiate this class, and then call the method to implement it. The development is not very efficient, and the extension method is different.
Haha, these are my gains. due to their limited capabilities, there may be many mistakes in this article. I hope that the experts who pass by and pass by will criticize and correct me a lot. I have a thick face. Even if I have any problems, I may point out that, thank you!



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.