using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
static class Program //必须是静态类才可以添加扩展方法
{
static void Main(string[] args)
{
string str = "ytu";
Console.WriteLine(str.add());//调用扩展方法,必须用对象来调用
Console.ReadKey();
}
//声明扩展犯非法,扩展方法必须是静态的
//add有三个参数,this必须有,string表示要扩展的对象,stringName表示对象名
//三个参数this和扩展的类型必不可少,对象名可以自己任意取
//如果需要传递参数,在增加一个变量即可
public static string add(this string stringName)
{
return stringName + "afdfdsa";
}
}
}
C#-----扩展方法
最新推荐文章于 2022-12-03 20:51:25 发布