網上這幾天正在瘋傳一段用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.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.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();
}
}
}
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();
}
}
}
說實話,感覺很奇怪。