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 and are rendered specially
92 instead of just using ToString ():
95 csharp> var pages = new Hashtable () {
96 > { "Mono", "http://www.mono-project.com/" },
97 > { "Linux", "http://kernel.org" } };
99 {{ "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> foreach (var old from f in Directory.GetFiles ("/tmp")
123 > let fi = new FileInfo (f)
124 > where fi.LastAccessTime < LastWeek select f)
125 > Console.WriteLine (old);
130 Unlike compiled C#, the type of a variable can be changed if a new
131 declaration is entered, for example:
135 csharp> a.GetType ();
137 csharp> var a = "Hello";
138 csharp> a.GetType ();
144 In the case that an expression or a statement is not completed in a
145 single line, a continuation prompt is displayed, for example:
148 csharp> var protocols = new string [] {
154 { "ftp", "http", "gopher" }
157 Long running computations can be interrupted by using the Control-C
161 csharp> var done = false;
162 csharp> while (!done) { }
164 System.Threading.ThreadAbortException: Thread was being aborted
165 at Class1.Host (System.Object& $retval) [0x00000]
166 at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
170 .SH INTERACTIVE EDITING
171 The C# interactive shell contains a line-editor that provides a more
172 advanced command line editing functionality than the operating system
175 The command set is similar to many other applications (cursor keys)
176 and incorporates some of the Emacs commands for editing as well as a
180 The following keyboard input is supported:
182 .I Home Key, Control-a
183 Goes to the beginning of the line.
185 .I End Key, Control-e
186 Goes to the end of the line.
188 .I Left Arrow Key, Control-b
189 Moves the cursor back one character.
191 .I Right Arrow Key, Control-f
192 Moves the cursor forward one character.
194 .I Up Arrow Key, Control-p
195 Goes back in the history, replaces the current line with the previous
198 .I Down Arrow Key, Control-n
199 Moves forward in the history, replaces the current line with the next
203 Executes the current line if the statement or expression is complete,
204 or waits for further input.
207 Cancel the current line being edited. This will kill any currently
208 in-progress edits or partial editing and go back to a toplevel
212 Deletes the character before the cursor
214 .I Delete Key, Control-d
215 Deletes the character at the current cursor position.
218 Erases the contents of the line until the end of the line and places
219 the result in the cut and paste buffer.
222 Deletes the word starting at the cursor position and appends into the
223 cut and paste buffer. By pressing Alt-d repeatedly, multiple words
224 can be appended into the paste buffer.
227 Pastes the content of the kill buffer at the current cursor position.
230 This is the quote character. It allows the user to enter
231 control-characters that are otherwise taken by the command editing
232 facility. Press Control-Q followed by the character you want to
233 insert, and it will be inserted verbatim into the command line.
234 .SH STATIC PROPERTIES AND METHODS
235 Since the methods and properties of the base class from where the
236 statements and expressions are executed are static, they can be
237 invoked directly from the shell. These are the available properties
240 .I void LoadPackage(string package)
241 Imports the package specified. This is equivalent to invoking the
242 compiler with the -pkg: flag with the specified string.
244 .I string Prompt { get; set }
245 The prompt used by the shell. It defaults to the value "csharp> ".
246 .I string ContinuationPrompt { get; set; }
247 The prompt used by the shell when further input is required to
248 complete the expression or statement.
251 Displays all the variables that have been defined so far and their
252 types. In the csharp shell declaring new variables will shadow
253 previous variable declarations, this is different than C# when
256 Displays all the using statements in effect.
258 The C# shell will load all the C# script files and Mono assemblies
259 located in the ~/.config/csharp directory on Unix. C# script files
260 are files that have the extension .cs and they should only contain
261 statements and expressions, they can not contain full class
262 definitions (at least not as of Mono 2.0). Full class definitions
263 should be compiled into dlls and stored in that directory.
265 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
266 Martin Baulig, Marek Safar and Raja Harinath. The development was
267 funded by Ximian, Novell and Marek Safar.
269 The Mono Compiler Suite is released under the terms of the GNU GPL or
270 the MIT X11. Please read the accompanying `COPYING' file for details.
271 Alternative licensing for the compiler is available from Novell.
273 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
275 To report bugs in the compiler, you must file them on our bug tracking
277 http://www.mono-project.com/Bugs
279 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
281 The Mono C# compiler was developed by Novell, Inc
282 (http://www.novell.com, http) and is based on the
283 ECMA C# language standard available here:
284 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
286 The home page for the Mono C# compiler is at
287 http://www.mono-project.com/CSharp_Compiler information about the
288 interactive mode for C# is available in http://mono-project.com/CsharpRepl