C#扩展方法

    class Program
{
static void Main(string[] args)
{
string word = "10";
int a = word.CustomToInt();
Console.WriteLine(a);
}
}
/// <summary>
/// 扩展方法(类和方法均为static)
/// 使用的时候要引用该类的命名空间
/// </summary>
public static class Demo
{
// this代表扩展方法应用于string类型上
// ToInt32()是将string类型转换为int类型的扩展方法
public static int CustomToInt(this string a)
{
int i;
Int32.TryParse(a,out i);
return i;
}
}
原文地址:https://www.cnblogs.com/kelei12399/p/2349816.html