C# 关于扩展方法
- 方法调用区代码
using ConsoleApp2;// 添加命名空间引用,扩展方法和调用方法不在同一个命名空间时
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
#region 扩展方法
string str = “小毛驴”;
var res = str.getStringAdd(3, 6);
Console.WriteLine(res);
#endregion
}
}
}
2.方法创建区域代码
namespace ConsoleApp2
{
//1.创建静态类
static class StringEx
{
//2.创建静态方法
public static int getStringAdd(this String str,int a,int b)//3.this 关键字指定给哪个类添加的扩展方法,a,b为参数;
{
return a + b;
}
}
}