前提

    

    小编最近正在准备面试,也想在.NET平台上更进一步,趁着面试复习了一些知识,同时也收获到一些新的知识——扩展方法就是其中之一。小编会通过一个Demo展示扩展方法的用法,以及对比它给我们带来的好处!


定义


    先说一个熟悉的内容,LINQ表达式,其实它就是扩展方法的一种体现。Msdn上介绍C#中的扩展方法——扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。也就是对String,Int,DataRow,DataTable等这些类型的基础上添加一个或多个方法,而不需要去修改或者编译类型本身。

Demo


1、不使用扩展方法


//**********************************************
// 文 件 名:Program
// 命名空间:$rootnamespace$
// 内 容:
// 功 能:
// 文件关系:
// 作 者:贾文静
// 生成日期:2016/11/23 21:22:23
// 版 本 号:V1.0.0.0
// 修改日志:
// 版权说明:
//**********************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace extendfunction
{
class Program
{
static void Main(string[] args)
{
List<string> persons = new List<string>();
persons.Add("zhang san");
persons.Add("zhang san feng");
persons.Add("li si ");
persons.Add("wang wu");
persons.Add("wang liu");
persons.Add("li ba");
persons.Add("lao wu");
persons.Add("zhang xx");

var result = from p in persons select p;
expand ex = new expand();
ex.Print(result);
Console.ReadLine();
}
public class expand
{
public void Print(IEnumerable<string> ie)
{
IEnumerator<string> result = ie.GetEnumerator();
while (result.MoveNext())
{
Console.WriteLine(result.Current);
}
}
}
}


2、使用扩展方法


//**********************************************
// 文 件 名:Program
// 命名空间:$rootnamespace$
// 内 容:
// 功 能:
// 文件关系:
// 作 者:贾文静
// 生成日期:2016/11/23 21:22:23
// 版 本 号:V1.0.0.0
// 修改日志:
// 版权说明:
//**********************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace extendfunction
{
class Program
{
static void Main(string[] args)
{
List<string> persons = new List<string>();
persons.Add("zhang san");
persons.Add("zhang san feng");
persons.Add("li si ");
persons.Add("wang wu");
persons.Add("wang liu");
persons.Add("li ba");
persons.Add("lao wu");
persons.Add("zhang xx");

var result = from p in persons select p;
result.Print();
Console.ReadLine();
}
}
#region 扩展方法
public static class ExtraClass
{
public static void Print(this IEnumerable<string> ie)
{
IEnumerator<string> result = ie.GetEnumerator();
while (result.MoveNext())
{
Console.WriteLine(result.Current);
}
}
}
#endregion
}

扩展方法的书写格式




    说说扩展方法的格式吧。从上面的demo上不知道大家能不能看出点什么东西来?它是在静态类中定义了静态方法,然后在函数第一个参数必须使用this来修饰,之后的内容和我们写普通方法基本一致,所以说也是非常容易上手的。

 

使用拓展方法的优点


    从简单程度上来说,使用扩展方法明显比使用传统方式要简单很多,不需要实例化,修改方法也不需要重新修改和编译。



那为什么扩展方法没有得到很大的推广呢?(不足之处)

1、是静态方法,使用不当会造成“污染”;

2、不具有override的能力,不会重载原有的方法

3、扩展方法会被扩展类的同名方法覆盖,所以实现扩展方法我们需要承担随时被覆盖的风险

4、扩展方法不能访问被扩展类的私有成员

5、扩展方法只能用实例来调用,不能像普通的静态方法一样使用类名调用;

6、只有引入扩展方法所在的命名空间后,扩展方法才可以使用。


扩展方法的本质




1、将实例方法调用在编译期改变为静态类中的静态方法调用,实际上,它确实拥有静态方法所有具有的所有功能。

2、作用域是整个namespace可见的,并通过使用using namespace来导入其他命名空间中的扩展方法。

3、优先级:现有实例方法优先级最高,其次为最近的namespace下的静态类的静态方法,最后为较远的namespace下的静态类的静态方法。

4、是一种编译技术,注意与反射等运行时技术进行区别,并慎重使用。


【总结】


    关于扩展方法学习起来还是非常简单的,容易上手易操作,所以敲个小Demo还是非常容易的,这个扩展方法使用起来非常的符合开闭原则,但是静态方法有让它多了一些危险性,所以真正想要使用扩展方法的时候还是需要考虑均衡。当然面对LINQ语句就不用担心,毕竟这是封装过得,用起来还是非常简单的。