扩展方法

扩展方法

一、概念

扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。 最常见的扩展方法是 LINQ 标准查询运算符


二、扩展方法的实现

1. 实现扩展方法的类必须是静态类且类的名称和实现扩展方法的类无关;

2. 实现扩展方法的类方法必须是静态方法;

3. 实现扩展方法的类方法的第一个参数必须是使用this关键字指明要实现扩展方法的类。

4. 实现扩展方法应遵守就近原则,在最小的范围内使用扩展方法以避免造成“污染”


三、扩展方法特点

1.扩展方法扩展自哪个类型,就必须是此类型的变量来使用,其他类型无法使用,本例扩展自DateTime类型,就只能是被DateTime类型的变量.出来(now.DateToString())

2.扩展方法中的this后面的参数不属于方法的参数,本例是无参数,this后面的DateTimedt是指明扩展方法扩展自何种类型

3.如果扩展方法和实例方法具有相同的签名,则优先调用实例方法

4.扩展自父类上的方法,可以被子类的对象直接使用

5.扩展自接口上的方法,可以被实现类的对象直接使用

6.扩展方法最终还是被编译器编译成:静态类.静态方法(),本例中now.DateToString()最终还是会被编译成DateHelper.DateToString(now),这是它的本质

Program.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

 

namespaceExtensionMethods3

{

    class Program

    {

        static void Main(string[] args)

        {

            DateTime now = DateTime.Now;

            //string time =now.ToString("yyyy-mm-dd hh:mm:ss");

            //string time =DateHelper.DateToString(now);

            string time = now.DateToString();

            Console.WriteLine(time);

            Console.ReadKey();

        }

    }

}

DateHelper.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

 

namespaceExtensionMethods3

{

    //改为静态类

    static class DateHelper

    {

        public static string DateToString(thisDateTime dt)

        {

            return dt.ToString("yyyy-mm-ddhh:mm:ss");

        }

    }

}


四、实例

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

//给自定义的类型增加一个扩展方法,并增加一个传递的参数

namespace ExtensionMethods1

{

    class Program

    {

        static voidMain(string[] args)

        {

            Studentnewstudent = new Student();

            //需用对象调用扩展方法

            string stuinfo= newstudent.ExtensionStuInfo();

           Console.WriteLine(stuinfo);

            stringstuinformation = newstudent.ExtensionGetStuInfo("quzijing","20081766");

           Console.WriteLine(stuinformation);

           Console.ReadKey();

        }

    }

}  

Student.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ExtensionMethods1

{

    public class Student

    {

        public stringStuInfo()

        {

            return "学生基本信息";

        }

        public stringgetStuInfo(string stuName,string stuNum)

        {

            returnstring.Format("学生信息:\n" + "姓名:{0}\n" + "学号:{1}",stuName,stuNum);

        }

    }

}

ExtensionStudentInfo.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

/*

 * 该类包含一些想要扩展的方法

 * 声明两个Student类型的扩展方法

 * Student类型为自定义的类型

 */

namespace ExtensionMethods1

{

    public static classExtensionStudentInfo

    {

        //声明扩展方法

        //要扩展的方法必须是静态的方法,Add有三个参数

        //this 必须有,Student表示扩展的类型,stuName表示对象名

        public staticstring ExtensionStuInfo(this Student stuName)

        {

            returnstuName.StuInfo();

        }

        //声明扩展方法

        //三个参数this和扩展的类型必不可少,对象名可以自己随意取如果需要传递参数,在此我们增加了两个string类型的参数

        public staticstring ExtensionGetStuInfo(this Student student, string stuname, string stunum)

        {

            returnstudent.getStuInfo(stuname, stunum) + "\n读取完毕";

        }

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值