C#3.0特性之扩展方法

为string类型,加一个扩展方法,IsNullOrEmpty,事实上.net已经把这个扩展方法集成了
还可以设计一个过滤Email的扩展方法
class Program
{
    static void Main(string[] args)
    {
        string newString = null;
        if (newString.IsNullOrEmpty())
        {
            // Do Something
        }
    }
}
public static class Extensions
{
 
    public static bool IsNullOrEmpty(this string s)
    {
        return (s == null || s.Trim().Length == 0);
    }
public static bool
        IsValidEmailAddress(this string s)
    {
        Regex regex = new
          Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
        return regex.IsMatch(s);
    }
}
原文地址:https://www.cnblogs.com/lori/p/2088293.html