惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Timothy Ye

IIS7中Host WCF遇到的问题 - Timothy Ye 博客园字体美化小技巧 - Timothy Ye - 博客园 个人闲置物品挥泪处理(8月27日更新) - Timothy Ye Visual Studio IDE配色方案 HTC Touch Pro 的重力感应功能 - Timothy Ye C# IDE Mobile – Write your C# code anywhere! - Timothy Ye Visual Studio 2010 Beta1 is available for download 小黑降温记 - Timothy Ye Windows 7 新特性体验 - Timothy Ye Unity 学习笔记(3) -- 生命周期管理 - Timothy Ye 换了新鼠标 Unity 学习笔记(2) -- 配置文件的使用 - Timothy Ye Unity 学习笔记(1) -- Unity简介及简单使用 Microsoft My Phone 体验 SQL中的日期计算 - Timothy Ye 快速开启、屏蔽Vista下的UAC提示 Dynamic Plugins Manager (五) Plugins Manager 源码下载 Dynamic Plugins Manager (四) 插件及Demo源码下载 Dynamic Plugins Manager (三) Demo
扩展方法使用小结
Timothy Ye · 2009-09-28 · via 博客园 - Timothy Ye

随着.Net Framework一路走来,已经让广大开发人员体验到快速开发的甜头,这得益于.Net Framework为我们提供了更高层次的封装,开发人员不必关心底层的Win32 API及其繁琐的调用参数,而可以把大部分的经历放在对业务的分析和实现。随着.Net的不断革新,也引入了更多的特性,例如C# 2.0,就增加了匿名方法和迭代器,这些特性让我们的编码效率更高。随着C# 3.0的推出,引入了更多的新特性,包括:隐式类型局部变量、对象初始化器、Lambda表达式、扩展方法、匿名类型。

而这些特性,都为LINQ的推出,构建好了基础。其中一个比较不错的特性,就是扩展方法。扩展方法,顾名思义,就是在类型定义完成之后,再继续为其添加新的方法。这是相当方便的,比如对于一个已经封装好的Assembly来说,我们不用改动Assembly的代码,而通过扩展方法就能实现对其功能的扩展,而且在调用的时候,仅仅通过代码,你几乎判断不出这是扩展方法,还是Assembly本身的方法。

扩展方法在.Net 3.5中的应用也是非常普遍的,如果你仔细观察,就会发现我们常用的Linq to Object中的Where,Select,Average,Sum等方法,以及Linq to SQL中的Where,Select,Average,Sum等方法,其实都是扩展方法,他们分别定义于System.Linq.Enumerable类和System.Linq.Queryable类之中。

就扩展方法本身而言,也存在一些限制之处。对于编译器来说,如果扩展方法和被扩展类型的方法发生冲突的时候,在调用此方法的时候,究竟是调用被扩展类型的方法,还是扩展方法呢?通过一个例子,我们就能发现其中的区别。

   1:    class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              new TestClassA().Display("Test");
   6:              new TestClassB().Display("Test");
   7:              Console.ReadLine();
   8:          }
   9:      }
  10:   
  11:      class TestClassA
  12:      {
  13:          public void Display(int b)
  14:          {
  15:              Console.WriteLine("This is TestClassA.Display() ...");
  16:          }
  17:      }
  18:   
  19:      class TestClassB
  20:      {
  21:          public void Display(string s)
  22:          {
  23:              Console.WriteLine("This is TestClassB.Display() ...");
  24:          }
  25:      }
  26:   
  27:      static class TestClassExtention
  28:      {
  29:          static public void Display(this object o, string s)
  30:          {
  31:              Console.WriteLine("This is TestClassExtention.Display() ...");
  32:          }
  33:      }

程序输出:

ExtentionMethod

可以看出,TestClassA的方法和扩展方法并没有冲突,因为他们的方法签名是不一样的,而TestClassB的方法和扩展方法有冲突,因为他们的都是接受一个string类型的输入参数。从结果可以看到,程序对Display的方法调用,TestClassB本身的Display方法,要“优先”于扩展方法Display被调用。因此,类本身的方法如果满足调用条件,那么这个方法会被优先执行,只有在类当中无法找到同样参数的方法时,扩展方法才有机会被执行。所以,我们可以得出这样的结论:扩展方法的优先级较低,也即扩展方法不会覆盖同名的类本身的方法。

另外,还有一个比较明显的区别,就是扩展方法要远远弱于类本身的方法,比如,在一个类中,类的方法可以访问自己的非公有成员,而扩展方法做不到这一点。