惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - HAL9000

旧文重发:漫谈字符集和编码 用html5 canvas和JS写个数独游戏 如何破解.net软件 夜晚才能工作的程序员 公司让各部门写春联,这是我们部门写的对子 盖茨和乔布斯 利用简单的力学法则来模拟天体运行(2) 利用简单的力学法则来模拟天体运行 一个仿windows泡泡屏保的实现 informix cli 汉字乱码的解决办法 msdn版windows 8无法激活的解决办法 谁是DAC2.8和jet4.0的接班人? 一个长事务引起的血案——Informix 长事务回滚失败引起的阻塞故障处理 一些常见的星际旅行的科技名词与其"理论根据": 笔记:连接SCO上的FTP时为何那么慢 VC非托管代码调用webservice,并支持代理服务器设置 天哪,我遇上了一个windows api BUG! 并贡献一个完全支持代理服务器的http文件下载VC代码 找到锁域账号的元凶 求高手指点:VC中wininet的代理服务器问题,耗了两天了,没有一点办法
关于中文编程的段子的一个实现
HAL9000 · 2012-02-06 · via 博客园 - HAL9000

关于中文编程的段子的一个实现

Posted on 2012-02-06 15:03  HAL9000  阅读(5891)  评论()    收藏  举报

   网上这几天正在疯传一段用C#进行中文编程的段子,说一个程序员就职后,发现公司的大哥里把C#用中文进行了包装,不光是类,还有关键字也进行了中文化,正在苦恼是不是要继续在这个公司干下去。

   这位大哥这种精神是否可嘉还真不好评价。对于没有意义的事情执着追求,是可嘉呢还是不可嘉,估计还是要看评论的人是谁。不过,人家自己的执着,别人也确实无资格评价。

   还有所谓“意义”,恐怕也是因人而定义的。一个东西,对于为之付出了精力的人来说是有意义的,而对于其他人来说,即然与之没有交集,也就无资格置评。对于文中的小哥来说,喜欢的就留下搞搞明白,不喜欢的就走人吧。

    只是这段中文化的代码,很有意思,上午试着用C#的lamda实现了一下,就所看到的代码而言,基本算是都实现了,现在我也可以用中文编程了。

下面是中文编程的示例,基本与网上那个段子差不多。 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 中文编程
{
    class Program
    {
        static void Main(string[] args)
        {
            // 逻辑判断演示....
            判断.如果是(判断.真).则(() =>
            {
                Console.WriteLine("");
            }).否则(() => 
            {
                Console.WriteLine("");
            });
            // 遍历器演示.....
            登陆信息[] 所有登录信息 = new 登陆信息[100];
            // ....

            遍历器.计数遍历().从(0).到(99).每隔(1).执行((当前索引行) =>
            {
                所有登录信息[当前索引行] = new 登陆信息() { 姓名 = "姓名 " + 当前索引行.ToString() };
            });

            遍历器.枚举遍历<登陆信息>(所有登录信息).从(0).到(99).每隔(3).执行((当前索引行, 登录信息项) =>
            {
                Console.WriteLine(登录信息项);
            });

            数据库连接类 数据连接对象 = null;
            //异常处理........
            异常.对下列语句进行异常检测(() =>
            {
                数据连接对象 = 数据库连接类.连接到("10.1.138.35").用户名为("xxx").密码为("xxx");
                数据连接对象.打开连接();
               
                //... 
                throw new Exception("测试异常");

            })
            .发现异常则((异常对象) =>
            {
                //... 
                Console.WriteLine(异常对象);
            })
            .最终执行(() => {
                // ...
                数据连接对象.关闭连接();

            });
        }
    }

    public class 登陆信息
    {
        public string 姓名;
        public override string ToString()
        {
            return "姓名" + 姓名;
        }
    }

}

关键字的包装:-----------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 中文编程
{
    public class 判断
    {
        public const bool 真 = true;
        public const bool 假 = false;

        bool _b;

        public static 判断 如果是(bool 条件)
        {
            return new 判断(){_b = 条件};
        }

        public 判断 则(Action act)
        {
            if (_b)
            {
                act();
            }
            return this;
        }

        public 判断 否则(Action act)
        {
            if (!_b)
            {
                act();
            }
            return this;
        }
    }

    public class 遍历器
    {
        public static 枚举遍历器<T> 枚举遍历<T>(IEnumerable<T> 枚举集合)
        {
            return new 枚举遍历器<T>(枚举集合);
        }

        public static 计数遍历器 计数遍历()
        {
            return new 计数遍历器() { };
        }
    }

    public class 枚举遍历器<T>
    {
        protected IEnumerable<T> _set;
        protected int _iStartIndex;
        protected int _iEndIndex;
        protected int _Step;

        public 枚举遍历器(IEnumerable<T> 枚举集合)
        {
            this._set = 枚举集合;
        }

        public 枚举遍历器<T> 从(int 开始元素序号)
        {
            this._iStartIndex = 开始元素序号;
            return this;
        }

        public 枚举遍历器<T> 到(int 结束元素序号)
        {
            this._iEndIndex = 结束元素序号;
            return this;
        }

        public 枚举遍历器<T> 每隔(int 每隔步长)
        {
            this._Step = 每隔步长;
            return this;
        }

        public void 执行(Action<int, T> 循环体方法)
        {
            int i = 0;
            foreach (var e in _set)
            {
                if (i >= this._iStartIndex && i <= this._iEndIndex)
                {
                    if ((i - this._iStartIndex) % this._Step == 0)
                    {
                        循环体方法(i, e);
                    }
                }
                i++;

            }
        }
    }

    
    public class 计数遍历器
    {
        protected int _iStartIndex;
        protected int _iEndIndex;
        protected int _Step;
    
        public 计数遍历器 从(int 开始元素序号)
        {
            this._iStartIndex = 开始元素序号;
            return this;
        }

        public 计数遍历器 到(int 结束元素序号)
        {
            this._iEndIndex = 结束元素序号;
            return this;
        }

        public 计数遍历器 每隔(int 每隔步长)
        {
            this._Step = 每隔步长;
            return this;
        }

        public void 执行(Action<int> 循环体方法)
        {
            for (int i = this._iStartIndex; i <= this._iEndIndex; i += this._Step)
            {
                循环体方法(i);
            }
        }
    }

    public class 异常
    {
        Exception _ex = null;

        public static 异常 对下列语句进行异常检测(Action 正常执行程序)
        {
            try
            {
                正常执行程序();
                return new 异常() { _ex = null};
            }
            catch (Exception ex)
            {
                return new 异常() { _ex = ex};
            }
            
        }

        public 异常 发现异常则(Action<Exception> 异常处理程序)
        {
            if (this._ex != null)
            {
                异常处理程序(this._ex);
            }
            return this;
        }

        public 异常 最终执行(Action 最终处理程序)
        {
            最终处理程序();
            return this;
        }
    }

}

数据库连接的包装: 

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace 中文编程
{
    public class 数据库连接类
    {
        private string _sServer;
        private string _sUID;
        private string _sPassword;
        private string _sDBName;
        SqlConnection _sqlconn = null;

        public static 数据库连接类 连接到(string 服务器名)
        {
            return new 数据库连接类() { _sServer = 服务器名 };
        }

        public 数据库连接类 用户名为(string 用户名)
        {
            _sUID = 用户名;
            return this;
        }

        public 数据库连接类 密码为(string 密码)
        {
            _sPassword = 密码;
            return this;
        }

        public 数据库连接类 数据库为(string 数据库名)
        {
            _sDBName = 数据库名;
            return this;
        }

        public void 打开连接()
        {
            this._sqlconn = new SqlConnection(string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}"this._sServer, this._sDBName, this._sUID, this._sPassword));
            this._sqlconn.Open();
        }

        public void 关闭连接()
        {
            this._sqlconn.Close();
        }

    }
}

说实话,感觉很奇怪。