向现有的类型“添加”方法。例如:String Int Class DataTable等
class Program { static void Main(string[] args) { List<UserInfo> list = new List<UserInfo> { new UserInfo{ Name="zs",Age=10,Sex="男"}, new UserInfo{ Name="ls",Age=11,Sex="女"}, new UserInfo{ Name="ww",Age=12,Sex="男"}, }; var result = list.Where(it => it.Age > 10).ToList().ToJson(); Console.ReadLine(); } } public static class Extension { public static string ToJson(this object obj) { return JsonConvert.SerializeObject(obj); } }
class Program { static void Main(string[] args) { string beginTime = “10:30”; string endTime = “10:50”; bool b = beginTime.CompareTime(endTime); Console.ReadLine(); } } public static class Extension { public static bool CompareTime(this string str1, string str2) { DateTime d1 = Convert.ToDateTime(str1); DateTime d2 = Convert.ToDateTime(str2); if (DateTime.Compare(d1, d2) > 0) return true; // d1>d2 return false; } }