超精简C#程序代码

来源:互联网 发布:sql drop所有表 编辑:程序博客网 时间:2024/06/09 19:04
一直以来我们都希望我们的代码在不影响可读、可维护、可移植等条件下尽可能的短小精悍。

对于编程发烧友来说将代码的精简做极致,往往会比较变态,今天我也变了一把,时刻准备着各位拍砖。

      事情是这样的,有个朋友说他写了个彩票机先程序,然后群里开始讨论他的代码,后来谈到是否可以精简,有人说80行,有人说60行。问到笔者这里,我想应该10行左右,怎么样算一行呢,一个分号算一行吧,不包含命名空间。

      需求是这样的, 
      1、从1-33里随机取出7个数据,从小到大排列;
      2、再从1-16随机选中一个数字,作为特别号码,组成最后的彩票号码;
      3、用Windows应用程序,点击开始,滚动号码,点击停止,停止号码滚动。

      于是就动手开始写吧,从一开始15个分号,到12个分号,最后到8个分号,算是一个比较成型的代码吧:
  1. 1 using System;
  2. 2 using System.Windows.Forms;
  3. 3 using System.Collections.Generic;
  4. 4 using System.Linq;

  5. 6 class Program
  6. 7 {
  7. 8    static void Main(string[] args)
  8. 9    {
  9. 10        new Func<Form, Random, Form>((f, r) =>
  10. 11        {
  11. 12            f.Controls.AddRange(new Control[] { new Button { Text = "start", Top = 50 }, new Label { AutoSize = true } });
  12. 13            ((Button)f.Controls[0]).Click += (sender, e) =>{
  13. 14                new Action<int>((ni) =>
  14. 15                {
  15. 16                    ((Button)sender).Text = ((Button)sender).Text == "stop" ? "start" : "stop";
  16. 17                    while (f.Controls[0].Text == "stop") { f.Controls[1].Text = new int[33].Select(i => ni > 33 ? ni = 1 : ni++).Distinct().OrderBy(i => r.Next(33)).Take(7).OrderBy(i => i).ToList().Aggregate<int, string>(string.Empty, (s, i) => s + " - " + i.ToString()).Substring(3) + " = " + r.Next(1, 17).ToString(); Application.DoEvents(); }
  17. 18                })(1);};
  18. 19            return f;
  19. 20        })(new Form { Text = "xxxx" }, new Random()).ShowDialog();
  20. 21    }
  21. 22 }
  22. 23
复制代码
运行结果就是这样子:
      



      代码中,为了尽可能少的分号,将临时变量的定义放进了委托调用的参数中,为了使委托也变少,将Click事件处理程序委托中的临时变量直接用sender中的某个成员,于是有了以下7个分号的代码,就显得比较变态了。
  1. 1 class Program
  2. 2 {
  3. 3    static void Main(string[] args)
  4. 4    {
  5. 5        new Func<Form, Random, Form>((f, r) =>
  6. 6        {
  7. 7            f.Controls.AddRange(new Control[] { new Button { Text = "start", Top = 50 }, new Label { AutoSize = true } });/*分号甲*/
  8. 8            ((Button)f.Controls[0]).Click += (sender, e) =>{
  9. 9                ((Button)sender).Text = ((Button)sender).Text == "stop" ? "start" : "stop";/*分号乙*/
  10. 10                while (f.Controls[0].Text == "stop") { f.Controls[1].Text = new int[33].Select(i => ((Control)sender).Tag is int && (int)((Control)sender).Tag < 33 ? (int)(((Control)sender).Tag = ((int)((Control)sender).Tag) + 1) : (int)(((Control)sender).Tag = 0) + 1).Distinct().OrderBy(i => r.Next(33)).Take(7).OrderBy(i => i).ToList().Aggregate<int, string>(string.Empty, (s, i) => s + " - " + i.ToString()).Substring(3) + " = " + r.Next(1, 17).ToString();/*分号丙*/ Application.DoEvents();/*分号丁*/ }};/*分号戊*/
  11. 11            return f;/*分号己*/
  12. 12        })(new Form { Text = "xxxx" }, new Random()).ShowDialog();/*分号庚*/
  13. 13    }
  14. 14 }
  15. 15
复制代码
这是笔者目前能想到最精简的代码,运行效果和之前一致。

今天比较无聊,纯属娱乐,很多为了减少分号出现的伎俩并不适合在真实开发中使用。

如果有哪位朋友有更少分号的实现,欢迎共享之。
原创粉丝点击