网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#教程
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.C#中结构与类的区别.
.c#里面的namespace基础(一).
.datagridcolumnstyle重写,实现插.
.C# 3.0新特性初步研究 Part2:使用.
.C#和VB.net语法对比图.
.switch语句的“不准遍历”.
.C# Namespace.
.C# 3.0新特性初步研究 Part1:使用.
.C#语言初级入门(1).
.数据结构与算法(C#实现)系列---树.
.C#的多线程机制初探(1) .
.C# 3.0新特性初步研究 Part3:使用.
.数据结构与算法(C#实现)系列---演.
.C# 3.0新特性系列:隐含类型var.
.SUNWEN教程之----C#进阶(四).
.C#的前途如何?.
.数据结构与算法(C#实现)系列----.
.C# 2.0中泛型编程初级入门教程.
.C#编程实用技巧:轻松实现对文件.
.数据结构与算法(C#实现)系列---演.

C# 3.0新特性初步研究 Part2:使用扩展方法

发表日期:2006-6-11


扩展方法(Extension Method)
可以为已有的类型添加新的方法定义和实现,比如int类型目前没有一个名叫xxxyyy()的方法,
那么通过使用扩展方法,我们可以为int类型添加一个xxxyyy()方法。
这个有点类似于用来扩展系统功能的某些设计模式。

下面我们用代码来说话:
这是我们以前的写法:

   1public static class Extensions
 2{
 3    public static string CamelCase(string identifier)
 4{
 5            string newString = "";
 6            bool sawUnderscore = false;
 7
 8            foreach (char c in identifier)
 9            {
10                if ((newString.Length == 0) && Char.IsLetter(c))
11                    newString += Char.ToUpper(c);
12                else if (c == '_')
13                    sawUnderscore = true;
14                else if (sawUnderscore)
15                {
16                        newString += Char.ToUpper(c);
17                        sawUnderscore = false;
18                }
19                else
20                        newString += c;
21        }
22
23            return newString;
24}           
25}
26
27static void Main(string[] args)
28{
29string[] identifiers = new string[] {
30         "do_something",
31         "find_all_objects",
32          "get_last_dict_entry"
33         };
34
35foreach (string s in identifiers)
36     Console.WriteLine("{0} becomes: {1}", s, Extensions.CamelCase(s));
37}
38
C# 3.0中我们可以这样写:
 1public static class Extensions
 2{
 3    public static string CamelCase(this string identifier)
 4{
 5            string newString = "";
 6            bool sawUnderscore = false;
 7
 8            foreach (char c in identifier)
 9            {
10                if ((newString.Length == 0) && Char.IsLetter(c))
11                    newString += Char.ToUpper(c);
12                else if (c == '_')
13                    sawUnderscore = true;
14                else if (sawUnderscore)
15                {
16                        newString += Char.ToUpper(c);
17                        sawUnderscore = false;
18                }
19                else
20                        newString += c;
21        }
22
23            return newString;
24}           
25}
26
27static void Main(string[] args)
28{
29string[] identifiers = new string[] {
30         "do_something",
31         "find_all_objects",
32          "get_last_dict_entry"
33         };
34
35foreach (string s in identifiers)
36     Console.WriteLine("{0} becomes: {1}", s, Extensions.CamelCase(s));
37}
主要是下面这两个语句的变化:
1public static string CamelCase(this string identifier)
2Console.WriteLine("{0} becomes: {1}", s, s.CamelCase());
变量s原本是一个string类型,并没有CamelCase()方法,但是我们在CamelCase()方法的参数列表最前面加上一个this关键字,
则string s就拥有了一个新的方法CamelCase,很简单也很直接 :)

下面我们看一看一个稍微复杂一点的应用:
 1public static class Extensions
 2{
 3public static List<T> Combine<T>(this List<T> a, List<T> b)
 4{
 5    var newList = new List<T>(a);
 6    newList.AddRange(b);
 7    return newList;
 8}   
 9}
10
11static void Main(string[] args)
12{
13var odds = new List<int>();
14odds.Add(1);
15odds.Add(3);
16odds.Add(5);
17odds.Add(7);
18
19var evens = new List<int>();
20evens.Add(0);
21evens.Add(2);
22evens.Add(4);
23evens.Add(6);
24
25var both = odds.Combine(evens);
26Console.WriteLine("Contents of 'both' list:");
27foreach (int i in both)
28     Console.WriteLine(i);
29}
怎%

http://zc1984.cnblogs.com/archive/2006/06/10/422676.html

上一篇:C# 3.0新特性初步研究 Part1:使用隐含类型的本地变量 人气:12473
下一篇:C# 3.0新特性初步研究 Part3:使用拉姆达表达式 人气:11016
浏览全部C# 3.0的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐