2
0

csharp.1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. .de Sp \" Vertical space (when we can't use .PP)
  2. .if t .sp .5v
  3. .if n .sp
  4. ..
  5. .TH csharp 1 "4 September 2008"
  6. .SH NAME
  7. csharp, gsharp \- Interactive C# Shell
  8. .SH SYNOPSIS
  9. .B csharp [--attach PID]
  10. [options]
  11. .P
  12. .B gsharp [file1 [file2]]
  13. .SH DESCRIPTION
  14. The
  15. .I csharp
  16. is an interactive C# shell that allows the user to enter and evaluate
  17. C# statements and expressions from the command line. The regular
  18. .I mcs
  19. command line options can be used in this version of the compiler.
  20. .PP
  21. The
  22. .I gsharp
  23. command is a GUI version of the C# interpreter that uses Gtk# and
  24. provides an area to attach widgets as well. This version can be
  25. attached to other Gtk# applications in a safe way as it injects itself
  26. into the main loop of a Gtk# application, avoiding any problems
  27. arising from the multi-threaded nature of injecting itself into a
  28. target process.
  29. .PP
  30. This version allows a number of scripts to be specified in the command
  31. line.
  32. .SH OPTIONS
  33. .TP
  34. .I "\-\-attach"
  35. This is an advanced option and should only be used if you have a deep
  36. understanding of multi-threading. This option is availble on the
  37. .I csharp
  38. command and allows the compiler to be injected into other processes.
  39. This is done by injecting the C# shell in a separate thread that runs
  40. concurrently with your application. This means that you must take
  41. special measures to avoid crashing the target application while using
  42. it. For example, you might have to take the proper locks before
  43. issuing any commands that might affect the target process state, or
  44. sending commands through a method dispatcher.
  45. .SH OPERATION
  46. Once you launch the csharp command, you will be greeted with the
  47. interactive prompt:
  48. .PP
  49. .nf
  50. $ csharp
  51. Mono C# Shell, type "help;" for help
  52. Enter statements below.
  53. csharp>
  54. .fi
  55. .PP
  56. A number of namespaces are pre-defined with C# these include System,
  57. System.Linq, System.Collections and System.Collections.Generic.
  58. Unlike the compiled mode, it is possible to add new using statements
  59. as you type code, for example:
  60. .PP
  61. .nf
  62. csharp> new XmlDocument ();
  63. <interactive>(1,6): error CS0246: The type or namespace name `XmlDocument' could not be found. Are you missing a using directive or an assembly reference?
  64. csharp> using System.Xml;
  65. csharp> new XmlDocument ();
  66. System.Xml.XmlDocument
  67. .fi
  68. .PP
  69. Every time a command is typed, the scope of that command is one of a
  70. class that derives from the class Mono.CSharp.InteractiveBase. This
  71. class defines a number of static properties and methods. To display
  72. a list of available commands access the `help' property:
  73. .nf
  74. csharp> help;
  75. "Static methods:
  76. LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
  77. [...]
  78. ShowVars (); - Shows defined local variables.
  79. ShowUsing (); - Show active using decltions.
  80. help;
  81. "
  82. csharp>
  83. .fi
  84. .PP
  85. When expressions are entered, the C# shell will display the result of
  86. executing the expression:
  87. .PP
  88. .nf
  89. csharp> Math.Sin (Math.PI/4);
  90. 0.707106781186547
  91. csharp> 1+1;
  92. 2
  93. csharp> "Hello, world".IndexOf (',');
  94. 5
  95. .fi
  96. .PP
  97. The C# shell uses the ToString() method on the returned object to
  98. display the object, this sometimes can be limiting since objects that
  99. do not override the ToString() method will get the default behavior
  100. from System.Object which is merely to display their type name:
  101. .PP
  102. .nf
  103. csharp> var a = new XmlDocument ();
  104. csharp> a;
  105. System.Xml.Document
  106. csharp> csharp> a.Name;
  107. "#document"
  108. csharp>
  109. .fi
  110. .PP
  111. A few datatypes are handled specially by the C# interactive shell like
  112. arrays, System.Collections.Hashtable, objects that implement
  113. System.Collections.IEnumerable and IDictionary and are rendered
  114. specially instead of just using ToString ():
  115. .PP
  116. .nf
  117. csharp> var pages = new Hashtable () {
  118. > { "Mono", "http://www.mono-project.com/" },
  119. > { "Linux", "http://kernel.org" } };
  120. csharp> pages;
  121. {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
  122. .fi
  123. .PP
  124. It is possible to use LINQ directly in the C# interactive shell since
  125. the System.Linq namespace has been imported at startup. The
  126. following sample gets a list of all the files that have not been
  127. accessed in a week from /tmp:
  128. .PP
  129. .nf
  130. csharp> using System.IO;
  131. csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
  132. csharp> var old_files = from f in Directory.GetFiles ("/tmp")
  133. > let fi = new FileInfo (f)
  134. > where fi.LastAccessTime < LastWeek select f;
  135. csharp>
  136. .fi
  137. .PP
  138. You can of course print the results in a single statement as well:
  139. .PP
  140. .nf
  141. csharp> using System.IO;
  142. csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
  143. csharp> from f in Directory.GetFiles ("/tmp")
  144. > let fi = new FileInfo (f)
  145. > where fi.LastAccessTime < last_week select f;
  146. [...]
  147. csharp>
  148. .fi
  149. .PP
  150. LINQ and its functional foundation produce on-demand code for
  151. IEnumerable return values. For instance, the return value from a
  152. using `from' is an IEnumerable that is evaluated on demand. The
  153. automatic rendering of IEnumerables on the command line will trigger
  154. the IEnumerable pipeline to execute at that point instead of having
  155. its execution delayed until a later point.
  156. .PP
  157. If you want to avoid having the IEnumerable rendered at this point,
  158. simply assign the value to a variable.
  159. .PP
  160. Unlike compiled C#, the type of a variable can be changed if a new
  161. declaration is entered, for example:
  162. .PP
  163. .nf
  164. csharp> var a = 1;
  165. csharp> a.GetType ();
  166. System.Int32
  167. csharp> var a = "Hello";
  168. csharp> a.GetType ();
  169. System.String
  170. csharp> ShowVars ();
  171. string a = "Hello"
  172. .fi
  173. .PP
  174. In the case that an expression or a statement is not completed in a
  175. single line, a continuation prompt is displayed, for example:
  176. .PP
  177. .nf
  178. csharp> var protocols = new string [] {
  179. > "ftp",
  180. > "http",
  181. > "gopher"
  182. > };
  183. csharp> protocols;
  184. { "ftp", "http", "gopher" }
  185. .fi
  186. .PP
  187. Long running computations can be interrupted by using the Control-C
  188. sequence:
  189. .PP
  190. .nf
  191. csharp> var done = false;
  192. csharp> while (!done) { }
  193. Interrupted!
  194. System.Threading.ThreadAbortException: Thread was being aborted
  195. at Class1.Host (System.Object& $retval) [0x00000]
  196. at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
  197. csharp>
  198. .fi
  199. .PP
  200. .SH INTERACTIVE EDITING
  201. The C# interactive shell contains a line-editor that provides a more
  202. advanced command line editing functionality than the operating system
  203. provides. These are available in the command line version, the GUI
  204. versions uses the standard Gtk# key bindings.
  205. .PP
  206. The command set is similar to many other applications (cursor keys)
  207. and incorporates some of the Emacs commands for editing as well as a
  208. history mechanism to
  209. .PP
  210. .PP
  211. The following keyboard input is supported:
  212. .TP
  213. .I Home Key, Control-a
  214. Goes to the beginning of the line.
  215. .TP
  216. .I End Key, Control-e
  217. Goes to the end of the line.
  218. .TP
  219. .I Left Arrow Key, Control-b
  220. Moves the cursor back one character.
  221. .TP
  222. .I Right Arrow Key, Control-f
  223. Moves the cursor forward one character.
  224. .TP
  225. .I Up Arrow Key, Control-p
  226. Goes back in the history, replaces the current line with the previous
  227. line in the history.
  228. .TP
  229. .I Down Arrow Key, Control-n
  230. Moves forward in the history, replaces the current line with the next
  231. lien in the history.
  232. .TP
  233. .I Return
  234. Executes the current line if the statement or expression is complete,
  235. or waits for further input.
  236. .TP
  237. .I Control-C
  238. Cancel the current line being edited. This will kill any currently
  239. in-progress edits or partial editing and go back to a toplevel
  240. definition.
  241. .TP
  242. .I Backspace Key
  243. Deletes the character before the cursor
  244. .TP
  245. .I Delete Key, Control-d
  246. Deletes the character at the current cursor position.
  247. .TP
  248. .I Control-k
  249. Erases the contents of the line until the end of the line and places
  250. the result in the cut and paste buffer.
  251. .TP
  252. .I Alt-D
  253. Deletes the word starting at the cursor position and appends into the
  254. cut and paste buffer. By pressing Alt-d repeatedly, multiple words
  255. can be appended into the paste buffer.
  256. .TP
  257. .I Control-Y
  258. Pastes the content of the kill buffer at the current cursor position.
  259. .TP
  260. .I Control-Q
  261. This is the quote character. It allows the user to enter
  262. control-characters that are otherwise taken by the command editing
  263. facility. Press Control-Q followed by the character you want to
  264. insert, and it will be inserted verbatim into the command line.
  265. .TP
  266. .I Control-D
  267. Terminates the program. This terminates the input for the program.
  268. .SH STATIC PROPERTIES AND METHODS
  269. Since the methods and properties of the base class from where the
  270. statements and expressions are executed are static, they can be
  271. invoked directly from the shell. These are the available properties
  272. and methods:
  273. .TP
  274. .I void LoadAssembly(string assembly)
  275. Loads the given assembly. This is equivalent to passing the compiler
  276. the -r: flag with the specified string.
  277. .TP
  278. .I void LoadPackage(string package)
  279. Imports the package specified. This is equivalent to invoking the
  280. compiler with the -pkg: flag with the specified string.
  281. .TP
  282. .I string Prompt { get; set }
  283. The prompt used by the shell. It defaults to the value "csharp> ".
  284. .I string ContinuationPrompt { get; set; }
  285. The prompt used by the shell when further input is required to
  286. complete the expression or statement.
  287. .TP
  288. .I void ShowVars()
  289. Displays all the variables that have been defined so far and their
  290. types. In the csharp shell declaring new variables will shadow
  291. previous variable declarations, this is different than C# when
  292. compiled.
  293. .I void ShowUsing()
  294. Displays all the using statements in effect.
  295. .I TimeSpan Time (Action a)
  296. Handy routine to time the time that some code takes to execute. The
  297. parameter is an Action delegate, and the return value is a TimeSpan.
  298. For example:
  299. .PP
  300. .nf
  301. csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
  302. 0
  303. 1
  304. 2
  305. 3
  306. 4
  307. 00:00:00.0043230
  308. csharp>
  309. .fi
  310. .PP
  311. The return value is a TimeSpan, that you can store in a variable for
  312. benchmarking purposes.
  313. .SH GUI METHODS AND PROPERTIES
  314. In addition to the methods and properties available in the console
  315. version there are a handful of extra properties available on the GUI
  316. version. For example a "PaneContainer" Gtk.Container is exposed that
  317. you can use to host Gtk# widgets while prototyping or the "MainWindow"
  318. property that gives you access to the current toplevel window.
  319. .SH STARTUP FILES
  320. The C# shell will load all the Mono assemblies and C# script files
  321. located in the ~/.config/csharp directory on Unix. The assemblies are
  322. loaded before the source files are loaded.
  323. .PP
  324. C# script files are files
  325. that have the extension .cs and they should only contain statements
  326. and expressions, they can not contain full class definitions (at least
  327. not as of Mono 2.0). Full class definitions should be compiled into
  328. dlls and stored in that directory.
  329. .SH AUTHORS
  330. The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
  331. Martin Baulig, Marek Safar and Raja Harinath. The development was
  332. funded by Ximian, Novell and Marek Safar.
  333. .SH LICENSE
  334. The Mono Compiler Suite is released under the terms of the GNU GPL or
  335. the MIT X11. Please read the accompanying `COPYING' file for details.
  336. Alternative licensing for the compiler is available from Novell.
  337. .SH SEE ALSO
  338. gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
  339. .SH BUGS
  340. To report bugs in the compiler, you must file them on our bug tracking
  341. system, at:
  342. http://www.mono-project.com/Bugs
  343. .SH MAILING LIST
  344. The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
  345. .SH MORE INFORMATION
  346. The Mono C# compiler was developed by Novell, Inc
  347. (http://www.novell.com, http) and is based on the
  348. ECMA C# language standard available here:
  349. http://www.ecma.ch/ecma1/STAND/ecma-334.htm
  350. .PP
  351. The home page for the Mono C# compiler is at
  352. http://www.mono-project.com/CSharp_Compiler information about the
  353. interactive mode for C# is available in http://mono-project.com/CsharpRepl