今天在园子里面发现一篇文章超精简C#程序代码,看了之后很是震撼,花了好长时间基本看明白了。随后又有了一些新的想法。
最近一直在思考扩展方法的应用,与这篇文章中的想法一接合,可将代码进一步精简,并可增强可读性。
代码仅用了三个分号...
这是小弟在博客园的第一篇作品,不足之处,请批评。
今天在园子里面发现一篇文章超精简C#程序代码,看了之后很是震撼,花了好长时间基本看明白了。随后又有了一些新的想法。
最近一直在思考扩展方法的应用,与这篇文章中的想法一接合,可将代码进一步精简,并可增强可读性。先看代码(仅用了三个分号):

Program
static class Program


{

/**//// <summary>
/// 先执行命令,再返回自身
/// </summary>
public static T Do<T>(this T t, Action<T> action)

{
action(t);
return t;
}

static void Main(string[] args)

{
new Form()
.Do(f => f.Tag = new Random())

.Do(f => f.Controls.Add(new Label
{ AutoSize = true }))
.Do(f => f.Controls.Add(

new Button
{ Text = "start", Top = 50 }
.Do(button => button.Click += (sender, e) => button
.Do(_button => _button.Text = (_button.Text == "stop") ? "start" : "stop")
.Do(_b =>

{
while (f.Controls[1].Text == "stop" &&
(f.Controls[0] as Label)
.Do(
l => l.Text =
string.Join(" - ",
new string[33]
.Select((i, j) => (j + 1).ToString("d2"))
.OrderBy(i => (f.Tag as Random).Next(33))
.Take(7)
.OrderBy(i => i)
.ToArray()
)
+ " = " + (f.Tag as Random).Next(1, 17)
)
.Do(l => Application.DoEvents())
!= null

)
{ }
}
)
)//button.AddReturn、
)// AddControl(B=new Button)
)
.ShowDialog();
}
}
首先,看看这个扩展方法:
1
/**//// <summary>
2
/// 先执行命令,再返回自身
3
/// </summary>4
public static T Do<T>(this T t, Action<T> action)
5
{
6
action(t);
7
return t;
8
}
这个方法先让t做一件事action,然后返回自身,通过这个扩展可以把很多代码串起来,如:

DoExample
1
private static void DoExample()
2
{
3
//常规代码
4
Form form = new Form();
5
form.Text = "Text";
6
form.TopMost = true;
7
form.WindowState = FormWindowState.Maximized;
8
form.Tag = "Tag";
9
form.ShowDialog();
10
//精简代码
11
new Form()
12
.Do(f => f.Text = "Text")
13
.Do(f => f.TopMost = true)
14
.Do(f => f.WindowState = FormWindowState.Maximized)
15
.Do(f => f.Tag = "Tag")
16
.ShowDialog();
17
}
以上代码中,通过Do扩展将对form操作的多条语句串成一句。作为示例只串了5条,再串上更多也没问题。这样代码写起来精练清晰。
Do也可以夹杂上其它代码,但这样会影响代码清晰,如下:

DoExample_Bad
1
private static void DoExample_Bad()
2
{
3
new Form()
4
.Do(f => Console.WriteLine("ABC"))
5
.Do(f => Application.DoEvents())
6
.ShowDialog();
7
}
虽然也能执行,但看起来就累了。
明白了Do扩展 ,再来看看前面的代码,先去除一部分,看看整体结构:

Main
static void Main(string[] args)
{
new Form()
.Do(f => f.Tag = new Random())
.Do(f => f.Controls.Add(new Label { AutoSize = true }))
.Do(f => f.Controls.Add(new Button
)
.ShowDialog();
}
再来看看 Button 部分

new Button
1
new Button
{ Text = "start", Top = 50 }
2
.Do(button => button.Click += (sender, e) =>
3
button
4
.Do(_button => _button.Text = (_button.Text == "stop") ? "start" : "stop")
5
.Do(_b =>生成并显示)
6
)
再就是生成并显示,是一个循环
1
while (f.Controls[1].Text == "stop" &&
2
(f.Controls[0] as Label).Do(生成并显示).Do(l => Application.DoEvents()) != null //条件二
3
)
4
{}
条件二永远为真 ,加在这里为了节省分号。条件二Do里面的 生成并显示如下:

Code
1
l => l.Text =
2
string.Join(" - ",
3
new string[33]
4
.Select((i, j) => (j + 1).ToString("d2"))
5
.OrderBy(i => (f.Tag as Random).Next(33))
6
.Take(7)
7
.OrderBy(i => i)
8
.ToArray()
9
)
10
+ " = " + (f.Tag as Random).Next(1, 17)
这里用string.Join代替Aggregate, 显得清晰了一些。
使用Select((i,j)=>...)代替原文中Tag作为变量,也大大简化清晰了代码。
其它不多说了。
这篇文章主要目的是介绍扩展方法的一个应用:简化,清晰代码!
最后,谢谢对我的启发很大。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架