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 It is possible to use LINQ directly in the C# interactive shell since
91 the System.Linq namespace has been imported at startup. The
92 following sample gets a list of all the files that have not been
93 accessed in a week from /tmp:
96 csharp> using System.IO;
97 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
98 csharp> var old_files = from f in Directory.GetFiles ("/tmp")
99 > let fi = new FileInfo (f)
100 > where fi.LastAccessTime < LastWeek select f;
104 You can of course print the results in a single statement as well:
107 csharp> using System.IO;
108 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
109 csharp> foreach (var old from f in Directory.GetFiles ("/tmp")
110 > let fi = new FileInfo (f)
111 > where fi.LastAccessTime < LastWeek select f)
112 > Console.WriteLine (old);
117 Unlike compiled C#, the type of a variable can be changed if a new
118 declaration is entered, for example:
122 csharp> a.GetType ();
124 csharp> var a = "Hello";
125 csharp> a.GetType ();
131 In the case that an expression or a statement is not completed in a
132 single line, a continuation prompt is displayed, for example:
135 csharp> var protocols = new string [] {
141 { "ftp", "http", "gopher" }
144 Long running computations can be interrupted by using the Control-C
148 csharp> var done = false;
149 csharp> while (!done) { }
151 System.Threading.ThreadAbortException: Thread was being aborted
152 at Class1.Host (System.Object& $retval) [0x00000]
153 at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
157 .SH INTERACTIVE EDITING
158 The C# interactive shell contains a line-editor that provides a more
159 advanced command line editing functionality than the operating system
162 The command set is similar to many other applications (cursor keys)
163 and incorporates some of the Emacs commands for editing as well as a
167 The following keyboard input is supported:
169 .I Home Key, Control-a
170 Goes to the beginning of the line.
172 .I End Key, Control-e
173 Goes to the end of the line.
175 .I Left Arrow Key, Control-b
176 Moves the cursor back one character.
178 .I Right Arrow Key, Control-f
179 Moves the cursor forward one character.
181 .I Up Arrow Key, Control-p
182 Goes back in the history, replaces the current line with the previous
185 .I Down Arrow Key, Control-n
186 Moves forward in the history, replaces the current line with the next
190 Executes the current line if the statement or expression is complete,
191 or waits for further input.
194 Cancel the current line being edited. This will kill any currently
195 in-progress edits or partial editing and go back to a toplevel
199 Deletes the character before the cursor
201 .I Delete Key, Control-d
202 Deletes the character at the current cursor position.
205 Erases the contents of the line until the end of the line and places
206 the result in the cut and paste buffer.
209 Deletes the word starting at the cursor position and appends into the
210 cut and paste buffer. By pressing Alt-d repeatedly, multiple words
211 can be appended into the paste buffer.
214 Pastes the content of the kill buffer at the current cursor position.
217 This is the quote character. It allows the user to enter
218 control-characters that are otherwise taken by the command editing
219 facility. Press Control-Q followed by the character you want to
220 insert, and it will be inserted verbatim into the command line.
221 .SH STATIC PROPERTIES AND METHODS
222 Since the methods and properties of the base class from where the
223 statements and expressions are executed are static, they can be
224 invoked directly from the shell. These are the available properties
227 .I void LoadPackage(string package)
228 Imports the package specified. This is equivalent to invoking the
229 compiler with the -pkg: flag with the specified string.
231 .I string Prompt { get; set }
232 The prompt used by the shell. It defaults to the value "csharp> ".
233 .I string ContinuationPrompt { get; set; }
234 The prompt used by the shell when further input is required to
235 complete the expression or statement.
238 Displays all the variables that have been defined so far and their
239 types. In the csharp shell declaring new variables will shadow
240 previous variable declarations, this is different than C# when
243 Displays all the using statements in effect.
245 The C# shell will load all the C# script files and Mono assemblies
246 located in the ~/.config/csharp directory on Unix. C# script files
247 are files that have the extension .cs and they should only contain
248 statements and expressions, they can not contain full class
249 definitions (at least not as of Mono 2.0). Full class definitions
250 should be compiled into dlls and stored in that directory.
252 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
253 Martin Baulig, Marek Safar and Raja Harinath. The development was
254 funded by Ximian, Novell and Marek Safar.
256 The Mono Compiler Suite is released under the terms of the GNU GPL or
257 the MIT X11. Please read the accompanying `COPYING' file for details.
258 Alternative licensing for the compiler is available from Novell.
260 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
262 To report bugs in the compiler, you must file them on our bug tracking
264 http://www.mono-project.com/Bugs
266 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
268 The Mono C# compiler was developed by Novell, Inc
269 (http://www.novell.com, http) and is based on the
270 ECMA C# language standard available here:
271 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
273 The home page for the Mono C# compiler is at
274 http://www.mono-project.com/CSharp_Compiler information about the
275 interactive mode for C# is available in http://mono-project.com/CsharpRepl