c#扩展方法使用说明

1、扩展方法只能在非泛型的静态类中使用。

2、扩展方法在静态类中只能以静态方法方式存在。   

3、静态类不能作为返回类型,静态类中不能包含实例化成员。

4、扩展方法中的参数前需要this关键字。

 

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("());

            Console.ReadKey();
        }
    }
    static class StirngHelper
    {
        public static bool isEmail(this string s)
        {
            return s.Contains("@");
        }
        public static string toUpper(this string s)
        {
            return s.ToUpper();
        }
        public static string quate(this string s)
        {
            return "[" "]";
        }
    }

 

 

namespace 静态扩展方法测试
{
    class Program
    {
        static void Main(string[] args)
        {

            //person new person();
            //p.getp1().getp1().getp1().getp1;

            "zz".getp3().getp1().getp2().gets1().getp3().gets1().getp3();

            new A().getA1().getA2().getA1().getA2().getAs().ToLower();

            new A().Len.getp3().getp1();


            Console.ReadKey();
        }
    }
    class A
    {
        public getA1()
        {
            return this;
        }
        public getA2()
        {
            return this;
        }
        public string getAs()
        {
            return "getAs";
        }
        public string Len="len";
    }
    static class person
    {
        public static chinese getp1(this chinese p)
        {
            Console.WriteLine("person getp1");
            return new chinese();
        }
        public static chinese getp2(this chinese p)
        {
            Console.WriteLine("person getp2");
            return new chinese();
        }
        public static string gets1(this chinese p)
        {
            Console.WriteLine("person gets1");
            return "person gets1";
        }
        public static chinese getp3(this string s)
        {
            Console.WriteLine("person getp3");
            return new chinese();
        }

        //public static string getp1s(this string s)
        //{
        //    return "|"+s+"|";
        //}
        //public static string getp2s(this string s)
        //{
        //    return "[" "]";
        //}
    }
    class chinese
    {

    }

    //class Program
    //{
    //    static void Main(string[] args)
    //    {
    //        Console.WriteLine("());

    //        Console.ReadKey();
    //    }
    //}
    //static class StirngHelper
    //{
    //    public static bool isEmail(this string s)
    //    {
    //        return s.Contains("@");
    //    }
    //    public static string toUpper(this string s)
    //    {
    //        return s.ToUpper();
    //    }
    //    public static string quate(this string s)
    //    {
    //        return "[" "]";
    //    }
    //}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值