浅谈 C# 交互窗口
时间:2010-11-30 来源:银河
C# 语言虽然不是动态语言,但是在 mono 中也有一个 C# 交互窗口: csharp,请参阅 CsharpRepl 。
ben@ben1520:~> 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; ben@ben1520:~>
可以看出,直接输入一个表达式,C# 交互窗口就会给出该表达式的值。使用 help 命令可以获得帮助:
ben@ben1520:~> 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;
ben@ben1520:~>
下面是使用 Linq 语句的例子:
ben@ben1520:~> 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/asound.state", "/etc/fstab", "/etc/group", "/etc/ld.so.cache", "/etc/mtab", "/etc/passwd", "/etc/passwd.old", "/etc/resolv.conf", "/etc/shadow" }
csharp> quit;
ben@ben1520:~>
此外,C# 交互窗口还支持编辑命令行,<Home>键移动光标到行首,<End>键移动光标到行末,<上箭头>键将当前行替换为上一行,<下箭头>键将当前行替换为下一行,<退格>键删除光标前一个字符,<Del>键删除光标处字符,<Tab>键补全命令行。更详细的信息可使用 man csharp 命令查看。
ben@ben1520:~/work> cat test.cs using System.Numerics; var a = 13; BigInteger.Pow(a, 53); ben@ben1520:~/work> csharp test.cs -r:System.Numerics.dll 109395050288514219040366056833567359441133418834382438145053 ben@ben1520:~/work> csharp --version Mono C# compiler version 4.0.0.0 ben@ben1520:~/work>
可见也可以将 C# 语句放到 .cs 文件中由 csharp 来解释执行。
此外,C# 交互窗口还有 GUI 版本:gsharp,可以使用 GTK#,并且内置一个 Plot 用来画函数的图像:
相关阅读 更多 +
排行榜 更多 +










