您的位置:首页 > 编程语言 > C#

C#3.0特性之扩展方法

2011-06-23 17:13 405 查看
为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

[code] IsValidEmailAddress(this string s)
{
       Regex regex = new
         Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
       return regex.IsMatch(s);
}

[/code]
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: