标签:
演示代码:
public class Program { static void Main(string[] args) { var txt = "hello"; Console.WriteLine(txt.GetString()); var inttxt = "123"; Console.WriteLine(inttxt.GetInt()); var inttxt1 = "asd"; Console.WriteLine(inttxt1.GetInt()); Console.WriteLine("234".GetInt()); Console.WriteLine("qwe".GetInt()); } } public static class ExString { public static string GetString(this string source) { return source + "扩展方法"; } public static int GetInt(this string source) { var outint = 0; if (!string.IsNullOrEmpty(source)) { var result = int.TryParse(source,out outint); return outint; } return outint; } }
代码输出:
说明:
1、扩展方法是属于静态类的静态方法
2、第一个参数 this关键字,参数类型是需要扩展的类型
标签:
原文地址:http://www.cnblogs.com/fuyujian/p/4394305.html