1 .de Sp \" Vertical space (when we can't use .PP)
5 .TH csharp 1 "4 September 2008"
7 csharp \- Interactive C# Shell
14 is an interactive C# shell that allows the user to enter and evaluate
15 C# statements and expressions from the command line.
17 This command is a shortcut to invoke the
21 command line option. All of the regular compiler options available
22 to gmcs are available in the interactive shell.
24 Once you launch the csharp command, you will be greeted with the
29 Mono C# Shell, type "help;" for help
31 Enter statements below.
35 A number of namespaces are pre-defined with C# these include System,
36 System.Linq, System.Collections and System.Collections.Generic.
37 Unlike the compiled mode, it is possible to add new using statements
38 as you type code, for example:
41 csharp> new XmlDocument ();
42 <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?
43 csharp> using System.Xml;
44 csharp> new XmlDocument ();
45 System.Xml.XmlDocument
48 Every time a command is typed, the scope of that command is one of a
49 class that derives from the class Mono.CSharp.InteractiveBase. This
50 class defines a number of static properties and methods. To display
51 a list of available commands access the `help' property:
55 LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
57 ShowVars (); - Shows defined local variables.
58 ShowUsing (); - Show active using decltions.
64 When expressions are entered, the C# shell will display the result of
65 executing the expression:
68 csharp> Math.Sin (Math.PI/4);
72 csharp> "Hello, world".IndexOf (',');
76 The C# shell uses the ToString() method on the returned object to
77 display the object, this sometimes can be limiting since objects that
78 do not override the ToString() method will get the default behavior
79 from System.Object which is merely to display their type name:
82 csharp> var a = new XmlDocument ();
85 csharp> csharp> a.Name;
90 A few datatypes are handled specially by the C# interactive shell like
91 arrays, System.Collections.Hashtable, objects that implement
92 System.Collections.IEnumerable and IDictionary and are rendered
93 specially instead of just using ToString ():
96 csharp> var pages = new Hashtable () {
97 > { "Mono", "http://www.mono-project.com/" },
98 > { "Linux", "http://kernel.org" } };
100 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
103 It is possible to use LINQ directly in the C# interactive shell since
104 the System.Linq namespace has been imported at startup. The
105 following sample gets a list of all the files that have not been
106 accessed in a week from /tmp:
109 csharp> using System.IO;
110 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
111 csharp> var old_files = from f in Directory.GetFiles ("/tmp")
112 > let fi = new FileInfo (f)
113 > where fi.LastAccessTime < LastWeek select f;
117 You can of course print the results in a single statement as well:
120 csharp> using System.IO;
121 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
122 csharp> from f in Directory.GetFiles ("/tmp")
123 > let fi = new FileInfo (f)
124 > where fi.LastAccessTime < last_week select f;
129 LINQ and its functional foundation produce on-demand code for
130 IEnumerable return values. For instance, the return value from a
131 using `from' is an IEnumerable that is evaluated on demand. The
132 automatic rendering of IEnumerables on the command line will trigger
133 the IEnumerable pipeline to execute at that point instead of having
134 its execution delayed until a later point.
136 If you want to avoid having the IEnumerable rendered at this point,
137 simply assign the value to a variable.
139 Unlike compiled C#, the type of a variable can be changed if a new
140 declaration is entered, for example:
144 csharp> a.GetType ();
146 csharp> var a = "Hello";
147 csharp> a.GetType ();
153 In the case that an expression or a statement is not completed in a
154 single line, a continuation prompt is displayed, for example:
157 csharp> var protocols = new string [] {
163 { "ftp", "http", "gopher" }
166 Long running computations can be interrupted by using the Control-C
170 csharp> var done = false;
171 csharp> while (!done) { }
173 System.Threading.ThreadAbortException: Thread was being aborted
174 at Class1.Host (System.Object& $retval) [0x00000]
175 at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
179 .SH INTERACTIVE EDITING
180 The C# interactive shell contains a line-editor that provides a more
181 advanced command line editing functionality than the operating system
184 The command set is similar to many other applications (cursor keys)
185 and incorporates some of the Emacs commands for editing as well as a
189 The following keyboard input is supported:
191 .I Home Key, Control-a
192 Goes to the beginning of the line.
194 .I End Key, Control-e
195 Goes to the end of the line.
197 .I Left Arrow Key, Control-b
198 Moves the cursor back one character.
200 .I Right Arrow Key, Control-f
201 Moves the cursor forward one character.
203 .I Up Arrow Key, Control-p
204 Goes back in the history, replaces the current line with the previous
207 .I Down Arrow Key, Control-n
208 Moves forward in the history, replaces the current line with the next
212 Executes the current line if the statement or expression is complete,
213 or waits for further input.
216 Cancel the current line being edited. This will kill any currently
217 in-progress edits or partial editing and go back to a toplevel
221 Deletes the character before the cursor
223 .I Delete Key, Control-d
224 Deletes the character at the current cursor position.
227 Erases the contents of the line until the end of the line and places
228 the result in the cut and paste buffer.
231 Deletes the word starting at the cursor position and appends into the
232 cut and paste buffer. By pressing Alt-d repeatedly, multiple words
233 can be appended into the paste buffer.
236 Pastes the content of the kill buffer at the current cursor position.
239 This is the quote character. It allows the user to enter
240 control-characters that are otherwise taken by the command editing
241 facility. Press Control-Q followed by the character you want to
242 insert, and it will be inserted verbatim into the command line.
245 Terminates the program. This terminates the input for the program.
246 .SH STATIC PROPERTIES AND METHODS
247 Since the methods and properties of the base class from where the
248 statements and expressions are executed are static, they can be
249 invoked directly from the shell. These are the available properties
252 .I void LoadAssembly(string assembly)
253 Loads the given assembly. This is equivalent to passing the compiler
254 the -r: flag with the specified string.
256 .I void LoadPackage(string package)
257 Imports the package specified. This is equivalent to invoking the
258 compiler with the -pkg: flag with the specified string.
260 .I string Prompt { get; set }
261 The prompt used by the shell. It defaults to the value "csharp> ".
262 .I string ContinuationPrompt { get; set; }
263 The prompt used by the shell when further input is required to
264 complete the expression or statement.
267 Displays all the variables that have been defined so far and their
268 types. In the csharp shell declaring new variables will shadow
269 previous variable declarations, this is different than C# when
272 Displays all the using statements in effect.
273 .I TimeSpan Time (Action a)
274 Handy routine to time the time that some code takes to execute. The
275 parameter is an Action delegate, and the return value is a TimeSpan.
279 csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
289 The return value is a TimeSpan, that you can store in a variable for
290 benchmarking purposes.
292 The C# shell will load all the C# script files and Mono assemblies
293 located in the ~/.config/csharp directory on Unix. C# script files
294 are files that have the extension .cs and they should only contain
295 statements and expressions, they can not contain full class
296 definitions (at least not as of Mono 2.0). Full class definitions
297 should be compiled into dlls and stored in that directory.
299 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
300 Martin Baulig, Marek Safar and Raja Harinath. The development was
301 funded by Ximian, Novell and Marek Safar.
303 The Mono Compiler Suite is released under the terms of the GNU GPL or
304 the MIT X11. Please read the accompanying `COPYING' file for details.
305 Alternative licensing for the compiler is available from Novell.
307 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
309 To report bugs in the compiler, you must file them on our bug tracking
311 http://www.mono-project.com/Bugs
313 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
315 The Mono C# compiler was developed by Novell, Inc
316 (http://www.novell.com, http) and is based on the
317 ECMA C# language standard available here:
318 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
320 The home page for the Mono C# compiler is at
321 http://www.mono-project.com/CSharp_Compiler information about the
322 interactive mode for C# is available in http://mono-project.com/CsharpRepl