我们知道,F# 的编译器是 fsc.exe,F# 交互窗口是 fsi.exe。其他动态语言也可以交互执行,例如 Ruby 语言的解释器是 ruby,交互窗口是 irb。而 Python 语言中,python 既可以解释执行,也可以作为交互窗口。请参见我于2010年6月15日写的随笔“Ubuntu 中的编程语言(上)”。
C# 语言虽然不是动态语言,但是在 mono 中也有一个 C# 交互窗口: csharp,请参阅 CsharpRepl 。
[email protected]:~> csharp
Mono C# Shell, type "help;" for help
Enter statements below.
csharp> Environment.Version;
4.0.30319.1
csharp> Environment.OSVersion;
Unix 2.6.34.7
csharp> var pi = Math.PI;
csharp> pi / 2;
1.5707963267949
csharp> ShowVars();
double pi = 3.14159265358979
csharp> quit;
[email protected]:~>
可以看出,直接输入一个表达式,C# 交互窗口就会给出该表达式的值。使用 help 命令可以获得帮助:
[email protected]:~> csharp
Mono C# Shell, type "help;" for help
Enter statements below.
csharp> help;
"Static methods:
Describe (object) - Describes the object's type
LoadPackage (package); - Loads the given Package (like -pkg:FILE)
LoadAssembly (assembly) - Loads the given assembly (like -r:ASSEMBLY)
ShowVars (); - Shows defined local variables.
ShowUsing (); - Show active using declarations.
Prompt - The prompt used by the C# shell
ContinuationPrompt - The prompt for partial input
Time(() -> { }) - Times the specified code
quit; - You'll never believe it - this quits the repl!
help; - This help text
TabAtStartCompletes - Whether tab will complete even on emtpy lines
"
csharp> ShowUsing();
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
csharp> quit;
[email protected]:~>
下面是使用 Linq 语句的例子:
[email protected]:~> csharp
Mono C# Shell, type "help;" for help
Enter statements below.
csharp> using System.IO;
csharp> from f in Directory.GetFiles("/etc")
> let fi = new FileInfo(f)
> where fi.LastWriteTime > DateTime.Now.AddDays(-7)
> select f;
{ "/etc/adjtime", "/etc/fstab", "/etc/group","/etc/mtab", "/etc/passwd", "/etc/shadow" }
csharp> quit;
[email protected]:~>
此外,C# 交互窗口还支持编辑命令行,<Home>键移动光标到行首,<End>键移动光标到行末,<上箭头>键将当前行替换为上一行,<下箭头>键将当前行替换为下一行,<退格>键删除光标前一个字符,<Del>键删除光标处字符,<Tab>键补全命令行。更详细的信息可使用 man csharp 命令查看。
[email protected]:~/work> cat test.cs
using System.Numerics;
var a = 13;
BigInteger.Pow(a, 53);
[email protected]:~/work> csharp test.cs -r:System.Numerics.dll
109395050288514219040366056833567359441133418834382438145053
[email protected]:~/work> csharp --version
Mono C# compiler version 4.0.0.0
[email protected]:~/work>
可见也可以将 C# 语句放到 .cs 文件中由 csharp 来解释执行。
此外,C# 交互窗口还有 GUI 版本:gsharp,可以使用 GTK#,并且内置一个 Plot 用来画函数的图像: