首页 > 其他 > 详细

扩展方法

时间:2014-12-14 19:56:37      阅读:228      评论:0      收藏:0      [点我收藏+]

/声明扩展方法的步骤:类必须是static,方法是static,
//第一个参数是被扩展的对象,前面标注this。
//使用扩展方法的时候必须保证扩展方法类已经在当前代码中using

namespace 扩展方法
{
    //扩展方法必须是静态的
    public static class StringHelper
    {
        //扩展方法必须是静态的,第一个参数必须加上this
        public static bool IsEmail(this string _input)
        {
            return Regex.IsMatch(_input, @"^\w+@\w+\.\w+$");
        }

        //带多个参数的扩展方法
        //在原始字符串前后加上指定的字符
        public static string Quot(this string _input, string _quot)
        {
            return _quot + _input + _quot;
        }
    }
}

扩展方法

原文:http://www.cnblogs.com/zklmmm/p/4162885.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!