1 .de Sp \" Vertical space (when we can't use .PP)
5 .TH csharp 1 "4 September 2008"
7 csharp, gsharp \- Interactive C# Shell
9 .B csharp [--attach PID] [-e EXPRESSION] [file1 [file2]]
12 .B gsharp [file1 [file2]]
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
19 command line options can be used in this version of the compiler.
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
30 Files specified in the command line will be loaded and executed as
33 Starting with Mono 2.10, the
35 command can be used as an interpreter executed by executables flagged
36 with the Unix execute attribute. To do this, make the first line of
37 your C# source code look like this:
40 Console.WriteLine ("Hello, World");
43 The commands accept all of the commands that are available to the
45 command, so you can reference assemblies, specify paths, language
46 level and so on from the command line. In addition, the following
47 command line options are supported:
50 This is an advanced option and should only be used if you have a deep
51 understanding of multi-threading. This option is availble on the
53 command and allows the compiler to be injected into other processes.
54 This is done by injecting the C# shell in a separate thread that runs
55 concurrently with your application. This means that you must take
56 special measures to avoid crashing the target application while using
57 it. For example, you might have to take the proper locks before
58 issuing any commands that might affect the target process state, or
59 sending commands through a method dispatcher.
62 This will evaluate the specified C# EXPRESSION and exit
64 Once you launch the csharp command, you will be greeted with the
69 Mono C# Shell, type "help;" for help
71 Enter statements below.
75 A number of namespaces are pre-defined with C# these include System,
76 System.Linq, System.Collections and System.Collections.Generic.
77 Unlike the compiled mode, it is possible to add new using statements
78 as you type code, for example:
81 csharp> new XmlDocument ();
82 <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?
83 csharp> using System.Xml;
84 csharp> new XmlDocument ();
85 System.Xml.XmlDocument
88 Every time a command is typed, the scope of that command is one of a
89 class that derives from the class Mono.CSharp.InteractiveBase. This
90 class defines a number of static properties and methods. To display
91 a list of available commands access the `help' property:
95 LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
97 ShowVars (); - Shows defined local variables.
98 ShowUsing (); - Show active using decltions.
104 When expressions are entered, the C# shell will display the result of
105 executing the expression:
108 csharp> Math.Sin (Math.PI/4);
112 csharp> "Hello, world".IndexOf (',');
116 The C# shell uses the ToString() method on the returned object to
117 display the object, this sometimes can be limiting since objects that
118 do not override the ToString() method will get the default behavior
119 from System.Object which is merely to display their type name:
122 csharp> var a = new XmlDocument ();
125 csharp> csharp> a.Name;
130 A few datatypes are handled specially by the C# interactive shell like
131 arrays, System.Collections.Hashtable, objects that implement
132 System.Collections.IEnumerable and IDictionary and are rendered
133 specially instead of just using ToString ():
136 csharp> var pages = new Hashtable () {
137 > { "Mono", "http://www.mono-project.com/" },
138 > { "Linux", "http://kernel.org" } };
140 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
143 It is possible to use LINQ directly in the C# interactive shell since
144 the System.Linq namespace has been imported at startup. The
145 following sample gets a list of all the files that have not been
146 accessed in a week from /tmp:
149 csharp> using System.IO;
150 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
151 csharp> var old_files = from f in Directory.GetFiles ("/tmp")
152 > let fi = new FileInfo (f)
153 > where fi.LastAccessTime < LastWeek select f;
157 You can of course print the results in a single statement as well:
160 csharp> using System.IO;
161 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
162 csharp> from f in Directory.GetFiles ("/tmp")
163 > let fi = new FileInfo (f)
164 > where fi.LastAccessTime < last_week select f;
169 LINQ and its functional foundation produce on-demand code for
170 IEnumerable return values. For instance, the return value from a
171 using `from' is an IEnumerable that is evaluated on demand. The
172 automatic rendering of IEnumerables on the command line will trigger
173 the IEnumerable pipeline to execute at that point instead of having
174 its execution delayed until a later point.
176 If you want to avoid having the IEnumerable rendered at this point,
177 simply assign the value to a variable.
179 Unlike compiled C#, the type of a variable can be changed if a new
180 declaration is entered, for example:
184 csharp> a.GetType ();
186 csharp> var a = "Hello";
187 csharp> a.GetType ();
193 In the case that an expression or a statement is not completed in a
194 single line, a continuation prompt is displayed, for example:
197 csharp> var protocols = new string [] {
203 { "ftp", "http", "gopher" }
206 Long running computations can be interrupted by using the Control-C
210 csharp> var done = false;
211 csharp> while (!done) { }
213 System.Threading.ThreadAbortException: Thread was being aborted
214 at Class1.Host (System.Object& $retval) [0x00000]
215 at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
219 .SH INTERACTIVE EDITING
220 The C# interactive shell contains a line-editor that provides a more
221 advanced command line editing functionality than the operating system
222 provides. These are available in the command line version, the GUI
223 versions uses the standard Gtk# key bindings.
225 The command set is similar to many other applications (cursor keys)
226 and incorporates some of the Emacs commands for editing as well as a
230 The following keyboard input is supported:
232 .I Home Key, Control-a
233 Goes to the beginning of the line.
235 .I End Key, Control-e
236 Goes to the end of the line.
238 .I Left Arrow Key, Control-b
239 Moves the cursor back one character.
241 .I Right Arrow Key, Control-f
242 Moves the cursor forward one character.
244 .I Up Arrow Key, Control-p
245 Goes back in the history, replaces the current line with the previous
248 .I Down Arrow Key, Control-n
249 Moves forward in the history, replaces the current line with the next
253 Executes the current line if the statement or expression is complete,
254 or waits for further input.
257 Cancel the current line being edited. This will kill any currently
258 in-progress edits or partial editing and go back to a toplevel
262 Deletes the character before the cursor
264 .I Delete Key, Control-d
265 Deletes the character at the current cursor position.
268 Erases the contents of the line until the end of the line and places
269 the result in the cut and paste buffer.
272 Deletes the word starting at the cursor position and appends into the
273 cut and paste buffer. By pressing Alt-d repeatedly, multiple words
274 can be appended into the paste buffer.
277 Pastes the content of the kill buffer at the current cursor position.
280 This is the quote character. It allows the user to enter
281 control-characters that are otherwise taken by the command editing
282 facility. Press Control-Q followed by the character you want to
283 insert, and it will be inserted verbatim into the command line.
286 Terminates the program. This terminates the input for the program.
287 .SH STATIC PROPERTIES AND METHODS
288 Since the methods and properties of the base class from where the
289 statements and expressions are executed are static, they can be
290 invoked directly from the shell. These are the available properties
293 .I void LoadAssembly(string assembly)
294 Loads the given assembly. This is equivalent to passing the compiler
295 the -r: flag with the specified string.
297 .I void LoadPackage(string package)
298 Imports the package specified. This is equivalent to invoking the
299 compiler with the -pkg: flag with the specified string.
301 .I string Prompt { get; set }
302 The prompt used by the shell. It defaults to the value "csharp> ".
303 .I string ContinuationPrompt { get; set; }
304 The prompt used by the shell when further input is required to
305 complete the expression or statement.
308 Displays all the variables that have been defined so far and their
309 types. In the csharp shell declaring new variables will shadow
310 previous variable declarations, this is different than C# when
313 Displays all the using statements in effect.
314 .I TimeSpan Time (Action a)
315 Handy routine to time the time that some code takes to execute. The
316 parameter is an Action delegate, and the return value is a TimeSpan.
320 csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
330 The return value is a TimeSpan, that you can store in a variable for
331 benchmarking purposes.
332 .SH GUI METHODS AND PROPERTIES
333 In addition to the methods and properties available in the console
334 version there are a handful of extra properties available on the GUI
335 version. For example a "PaneContainer" Gtk.Container is exposed that
336 you can use to host Gtk# widgets while prototyping or the "MainWindow"
337 property that gives you access to the current toplevel window.
339 The C# shell will load all the Mono assemblies and C# script files
340 located in the ~/.config/csharp directory on Unix. The assemblies are
341 loaded before the source files are loaded.
343 C# script files are files
344 that have the extension .cs and they should only contain statements
345 and expressions, they can not contain full class definitions (at least
346 not as of Mono 2.0). Full class definitions should be compiled into
347 dlls and stored in that directory.
349 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
350 Martin Baulig, Marek Safar and Raja Harinath. The development was
351 funded by Ximian, Novell and Marek Safar.
353 The Mono Compiler Suite is released under the terms of the GNU GPL or
354 the MIT X11. Please read the accompanying `COPYING' file for details.
355 Alternative licensing for the compiler is available from Novell.
357 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
359 To report bugs in the compiler, you must file them on our bug tracking
361 http://www.mono-project.com/Bugs
363 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
365 The Mono C# compiler was developed by Novell, Inc
366 (http://www.novell.com, http) and is based on the
367 ECMA C# language standard available here:
368 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
370 The home page for the Mono C# compiler is at
371 http://www.mono-project.com/CSharp_Compiler information about the
372 interactive mode for C# is available in http://mono-project.com/CsharpRepl