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

推荐订阅源

T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题
T
Troy Hunt's Blog
N
News and Events Feed by Topic
雷峰网
雷峰网
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 三生石上(FineUI控件)
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 最新话题
V
V2EX
T
Threat Research - Cisco Blogs
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
I
Intezer
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
月光博客
月光博客
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
B
Blog
博客园 - 聂微东
P
Palo Alto Networks Blog
A
About on SuperTechFans
The Last Watchdog
The Last Watchdog
Scott Helme
Scott Helme
Google DeepMind News
Google DeepMind News
Webroot Blog
Webroot Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
O
OpenAI News
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
A
Arctic Wolf

博客园 - tonyYe

请求筛选模块被配置为拒绝包含双重转义序列的请求。HTTP 错误 404.11 - Not Found asp.net页面传值方法汇总 - tonyYe - 博客园 Syntax error on line 198 httpd.conf: ServerAdmin takes one argument ASP.NET 2.0中Page事件的执行顺序 {转} EM 无法启动,重新完全配置EM ToString - tonyYe - 博客园 快捷方式制作 ListView导出到Excel TreeView的NodeCheck联动 利用js去除打印时的页眉页脚 使用证书来做RSA非对称式加密 用证书实现windows 2003下IIS的SSL安全通信 Windows命令提示符大全 学习.net应该知道什么 针对构架师的.NET 3.0介绍 Microsoft .net 框架开发平台体系架构 MapGuide公共静态方法 查询保存在outlook里的用户名和密码 ASP.NET 2.0 GridView
C# 3.0新特性之扩展方法
tonyYe · 2008-06-12 · via 博客园 - tonyYe

C#3.0扩展方法是给现有类型添加一个方法。

//Demo--1 //扩展基本类型 namespace TestExtensionMethods { 
// 必须建一个静态类,用来包含要添加的扩展方法 public static class Extensions { 
//要添加的扩展方法必须为一个静态方法 
//此方法参数列表必须以this开始 第二个即为要扩展的数据类型,在这里就是要扩展string类型 
//第三个就无所谓了,就是一对象名,名字随便,符合命名规则即可 
//综合来讲,此方法就是要给string类型添加一个叫TestMethod的方法,此方法返回一个int型的值,即返回调用此方法对象的长度。 
public static int TestMethod(this string s) return s.Length; 
}
 

//测试扩展方法类 
class Program static void Main(string[] args) 

string str = "Hello Extension Methods"
//调用扩展方法,必须用对象来调用 
int len = str.TestMethod(); 
Console.WriteLine(len); 
}
 
}
 


//Demo--2 
//扩展自定义类型,同时展示了扩展方法带参数情况,以及方法重载 

namespace TestExtendMethod 

public class Student 

public string Description() 

return "Student."
}
 
public string Description(string name) return "the student’s name is "+name; 
}
 
}
 

// 必须建一个静态类,用来包含要添加的扩展方法 public static class Extensions 

//要添加的扩展方法必须为一个静态方法 
//此方法参数列表必须以this开始 第二个即为要扩展的数据类型,在这里就是要扩展Student类型 
//第三个就无所谓了,就是一对象名,名字随便,符合命名规则即可 
//综合来讲,此方法就是要给Student类型添加一个叫TestMethod的方法,此方法返回一个string型的值 public static string TestMethod(this Student s) 
return s.Description(); } 
//要添加的扩展方法必须为一个静态方法 
//此方法参数列表第一个参数表示要扩展哪一个类,第二个参数才表示此扩展方法的真正参数 
//综合来讲,此方法就是要给Student类型添加一个叫TestMethod的方法,此方法带有一个string类型的参数,并返回一个string型的值 
public static string TestMethod(this Student s,string name) 

return s.Description(name); 
}
 
}
 

//测试扩展方法类 
class Program static void Main(string[] args) { Student stu = new Student(); 
//调用扩展方法,必须用对象来调用 
string mes = stu.TestMethod(); Console.WriteLine(mes); 
//调用带参数的扩展方法,只要传第二个参数就可以了 
//因为他的第一个参数其实只是为了表明是扩展哪个数据类型 
mes = stu.TestMethod("abc"); 
Console.WriteLine(mes); 
}
 
}
 
}
 

现在类型既可是基本数据类型(如int,String等),也可以是自己定义的类。