扩展方法(Extension Method)
可以为已有的类型添加新的方法定义和实现,比如int类型目前没有一个名叫xxxyyy()的方法,
那么通过使用扩展方法,我们可以为int类型添加一个xxxyyy()方法。
这个有点类似于用来扩展系统功能的某些设计模式。
下面我们用代码来说话:
这是我们以前的写法:
C# 3.0中我们可以这样写:
主要是下面这两个语句的变化:
变量s原本是一个string类型,并没有CamelCase()方法,但是我们在CamelCase()方法的参数列表最前面加上一个this关键字,
则string s就拥有了一个新的方法CamelCase,很简单也很直接 :)
下面我们看一看一个稍微复杂一点的应用:
怎%
可以为已有的类型添加新的方法定义和实现,比如int类型目前没有一个名叫xxxyyy()的方法,
那么通过使用扩展方法,我们可以为int类型添加一个xxxyyy()方法。
这个有点类似于用来扩展系统功能的某些设计模式。
下面我们用代码来说话:
这是我们以前的写法:
1
public 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
27
static void Main(string[] args)
28
{
29
string[] identifiers = new string[] {
30
"do_something",
31
"find_all_objects",
32
"get_last_dict_entry"
33
};
34
35
foreach (string s in identifiers)
36
Console.WriteLine("{0} becomes: {1}", s, Extensions.CamelCase(s));
37
}
38

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

C# 3.0中我们可以这样写:
1
public 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
27
static void Main(string[] args)
28
{
29
string[] identifiers = new string[] {
30
"do_something",
31
"find_all_objects",
32
"get_last_dict_entry"
33
};
34
35
foreach (string s in identifiers)
36
Console.WriteLine("{0} becomes: {1}", s, Extensions.CamelCase(s));
37
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

主要是下面这两个语句的变化:
1
public static string CamelCase(this string identifier)
2
Console.WriteLine("{0} becomes: {1}", s, s.CamelCase());

2

变量s原本是一个string类型,并没有CamelCase()方法,但是我们在CamelCase()方法的参数列表最前面加上一个this关键字,
则string s就拥有了一个新的方法CamelCase,很简单也很直接 :)
下面我们看一看一个稍微复杂一点的应用:
1
public static class Extensions
2
{
3
public 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
11
static void Main(string[] args)
12
{
13
var odds = new List<int>();
14
odds.Add(1);
15
odds.Add(3);
16
odds.Add(5);
17
odds.Add(7);
18
19
var evens = new List<int>();
20
evens.Add(0);
21
evens.Add(2);
22
evens.Add(4);
23
evens.Add(6);
24
25
var both = odds.Combine(evens);
26
Console.WriteLine("Contents of 'both' list:");
27
foreach (int i in both)
28
Console.WriteLine(i);
29
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

怎%
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述