您的位置:首页 > 其它

扩展方法的使用

2016-11-24 22:08 127 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Test扩展方法

{

    static class StringExtend

    {

        public static bool IsEmal(this string s)//在static类中,在static方法中的第一个参数上加一个 this,就是对这个参数的类型的扩展。

        {

            if (s.Contains("@")) 

            { 

                return true;

            }

            else 

                return false;

        }

        public static string AddString(this string s,string str)//this必须加在第一个参数上面才能形成对第一个参数类型的扩展。

        {

            return s + str;

        }

    }
}

在main函数中调用

namespace Test扩展方法

{

    class Program

    {

        static void Main(string[] args)

        {

            string s = "123@qq.com";

            Console.WriteLine(s.IsEmal());

            Console.WriteLine(s.AddString("zzzz"));//带参数的扩展方法

            Console.ReadKey();

        }

    }

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