拓展方法

目的:为已存在类型添加方法。

注意:

1           方法所在的类必须是静态的
2          
方法也必须是静态的
3           方法的第一个参数必须是你要扩展的那个类型。
4           在第一个参数前面还需要有一个this
关键字。

二: Demo

 1 namespace 扩展方法
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             string aa = "123,";
 8             aa.deletelast();
 9 
10         }
11     }
12 
13     public static class Extensions
14     {
15         public static string deletelast(this System.String textMessage)
16         {
17             return textMessage.Substring(0, textMessage.Length - 1);
18         }
19     }
20 
21 
22 }
原文地址:https://www.cnblogs.com/zihunqingxin/p/3061251.html