分析一段超精简C#程序代码

2016-02-19 12:26 3 1 收藏

今天图老师小编给大家展示的是分析一段超精简C#程序代码,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

  一直以来我们都希望我们的代码在不影响可读、可维护、可移植等条件下尽可能的短小精悍。
      对于编程发烧友来说将代码的精简做极致,往往会比较变态,今天我也变了一把,时刻准备着各位拍砖。

      事情是这样的,有个朋友说他写了个彩票机先程序,然后群里开始讨论他的代码,后来谈到是否可以精简,有人说80行,有人说60行。问到笔者这里,我想应该10行左右,怎么样算一行呢,一个分号算一行吧,不包含命名空间。
      需求是这样的, 
      1、从1-33里随机取出7个数据,从小到大排列;
      2、再从1-16随机选中一个数字,作为特别号码,组成最后的彩票号码;
      3、用Windows应用程序,点击开始,滚动号码,点击停止,停止号码滚动。

      于是就动手开始写吧,从一开始15个分号,到12个分号,最后到8个分号,算是一个比较成型的代码吧:
   1 using System;
 2 using System.Windows.Forms;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 
 6 class Program
 7 {
 8     static void Main(string[] args)
 9     {
10         new FuncForm, Random, Form((f, r) =
11         {
12             f.Controls.AddRange(new Control[] { new Button { Text = "start", Top = 50 }, new Label { AutoSize = true } });
13             ((Button)f.Controls[0]).Click += (sender, e) ={
14                 new Actionint((ni) =
15                 {
16                     ((Button)sender).Text = ((Button)sender).Text == "stop" ? "start" : "stop";
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().Aggregateint, string(string.Empty, (s, i) = s + " - " + i.ToString()).Substring(3) + " = " + r.Next(1, 17).ToString(); Application.DoEvents(); }
18                 })(1);};
19             return f;
20         })(new Form { Text = "xxxx" }, new Random()).ShowDialog();
21     }
22 }
23 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

运行结果就是这样子:
      

      代码中,为了尽可能少的分号,将临时变量的定义放进了委托调用的参数中,为了使委托也变少,将Click事件处理程序委托中的临时变量直接用sender中的某个成员,于是有了以下7个分号的代码,就显得比较变态了。
      

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)  1 class Program
 2 {
 3     static void Main(string[] args)
 4     {
 5         new FuncForm, Random, Form((f, r) =
 6         {
 7             f.Controls.AddRange(new Control[] { new Button { Text = "start", Top = 50 }, new Label { AutoSize = true } });/*分号甲*/
 8             ((Button)f.Controls[0]).Click += (sender, e) ={
 9                 ((Button)sender).Text = ((Button)sender).Text == "stop" ? "start" : "stop";/*分号乙*/
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().Aggregateint, string(string.Empty, (s, i) = s + " - " + i.ToString()).Substring(3) + " = " + r.Next(1, 17).ToString();/*分号丙*/ Application.DoEvents();/*分号丁*/ }};/*分号戊*/
11             return f;/*分号己*/
12         })(new Form { Text = "xxxx" }, new Random()).ShowDialog();/*分号庚*/
13     }
14 }
15 

这是笔者目前能想到最精简的代码,运行效果和之前一致。
      今天比较无聊,纯属娱乐,很多为了减少分号出现的伎俩并不适合在真实开发中使用。
      如果有哪位朋友有更少分号的实现,欢迎共享之。

来源:https://www.tulaoshi.com/n/20160219/1600904.html

延伸阅读
标签: PHP
<?php   $key = "This is supposed to be a secret key !!!";   function keyED($txt,$encrypt_key)   {   $encrypt_key = md5($encrypt_key);   $ctr=0;   $tmp = "";   for ($i=0;$i<strlen($txt);$i++)   { ...
源文件内容: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace wzy2 {  /// <summary  /// Form1 的摘要说明。  /// </summary  public class Form1 : System.Windows.Forms.Form  {   private...
前言: 因为本程序是木马程序,所以在介绍之前有一些木马构成的基本知识事先说明,因为下面很多地方会提到这些内容。一个完整的木马系统由硬件部分,软件部分和具体连接部分组成。这里主要对软件部分介绍,它主要有控制端程序、木马程序(后台服务程序)、木马配制程序组成。控制端用以远程控制服务端的程序;木马程序是潜入服务端内...
标签: Web开发
.aaa { border:1px solid #ccc; background:#f60; height:200px; } 测试一下 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
标签: ASP
  <!-- Here we are calling the LPK file.  This LPK file was created using the same method explained in the article-- <object CLASSID="clsid:5220cb21-c88d-11cf-b347-00aa00a28331" id="Microsoft_Licensed_Class_Manager_1_0"    <param NAME="LPKPath" VALU...

经验教程

64

收藏

49
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部