1 namespace ExtensionMethod 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 //要求很简单,判断字符串是否长度大于三并且包含a 8 string str = "1bcb"; 9 if (str.CheckStr()) 10 { 11 Console.WriteLine("字符串正确"); 12 } 13 else { 14 Console.WriteLine("字符串有误"); 15 } 16 Console.Read(); 17 } 18 } 19 static class HelpString { 20 /// <summary> 21 /// 验证字符串参数长度是否大于3且含有“a” 22 /// </summary> 23 /// <param name="str"></param> 24 /// <returns></returns> 25 public static bool CheckStr(this string str) 26 { 27 if (str.Length <= 3 || str.IndexOf("a") == -1) 28 { 29 return false; 30 } 31 return true; 32 } 33 } 34 }
关键子就是this啦 . . .