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] [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
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
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.
46 Once you launch the csharp command, you will be greeted with the
51 Mono C# Shell, type "help;" for help
53 Enter statements below.
57 A number of namespaces are pre-defined with C# these include System,
58 System.Linq, System.Collections and System.Collections.Generic.
59 Unlike the compiled mode, it is possible to add new using statements
60 as you type code, for example:
63 csharp> new XmlDocument ();
64 <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?
65 csharp> using System.Xml;
66 csharp> new XmlDocument ();
67 System.Xml.XmlDocument
70 Every time a command is typed, the scope of that command is one of a
71 class that derives from the class Mono.CSharp.InteractiveBase. This
72 class defines a number of static properties and methods. To display
73 a list of available commands access the `help' property:
77 LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
79 ShowVars (); - Shows defined local variables.
80 ShowUsing (); - Show active using decltions.
86 When expressions are entered, the C# shell will display the result of
87 executing the expression:
90 csharp> Math.Sin (Math.PI/4);
94 csharp> "Hello, world".IndexOf (',');
98 The C# shell uses the ToString() method on the returned object to
99 display the object, this sometimes can be limiting since objects that
100 do not override the ToString() method will get the default behavior
101 from System.Object which is merely to display their type name:
104 csharp> var a = new XmlDocument ();
107 csharp> csharp> a.Name;
112 A few datatypes are handled specially by the C# interactive shell like
113 arrays, System.Collections.Hashtable, objects that implement
114 System.Collections.IEnumerable and IDictionary and are rendered
115 specially instead of just using ToString ():
118 csharp> var pages = new Hashtable () {
119 > { "Mono", "http://www.mono-project.com/" },
120 > { "Linux", "http://kernel.org" } };
122 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
125 It is possible to use LINQ directly in the C# interactive shell since
126 the System.Linq namespace has been imported at startup. The
127 following sample gets a list of all the files that have not been
128 accessed in a week from /tmp:
131 csharp> using System.IO;
132 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
133 csharp> var old_files = from f in Directory.GetFiles ("/tmp")
134 > let fi = new FileInfo (f)
135 > where fi.LastAccessTime < LastWeek select f;
139 You can of course print the results in a single statement as well:
142 csharp> using System.IO;
143 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
144 csharp> from f in Directory.GetFiles ("/tmp")
145 > let fi = new FileInfo (f)
146 > where fi.LastAccessTime < last_week select f;
151 LINQ and its functional foundation produce on-demand code for
152 IEnumerable return values. For instance, the return value from a
153 using `from' is an IEnumerable that is evaluated on demand. The
154 automatic rendering of IEnumerables on the command line will trigger
155 the IEnumerable pipeline to execute at that point instead of having
156 its execution delayed until a later point.
158 If you want to avoid having the IEnumerable rendered at this point,
159 simply assign the value to a variable.
161 Unlike compiled C#, the type of a variable can be changed if a new
162 declaration is entered, for example:
166 csharp> a.GetType ();
168 csharp> var a = "Hello";
169 csharp> a.GetType ();
175 In the case that an expression or a statement is not completed in a
176 single line, a continuation prompt is displayed, for example:
179 csharp> var protocols = new string [] {
185 { "ftp", "http", "gopher" }
188 Long running computations can be interrupted by using the Control-C
192 csharp> var done = false;
193 csharp> while (!done) { }
195 System.Threading.ThreadAbortException: Thread was being aborted
196 at Class1.Host (System.Object& $retval) [0x00000]
197 at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
201 .SH INTERACTIVE EDITING
202 The C# interactive shell contains a line-editor that provides a more
203 advanced command line editing functionality than the operating system
204 provides. These are available in the command line version, the GUI
205 versions uses the standard Gtk# key bindings.
207 The command set is similar to many other applications (cursor keys)
208 and incorporates some of the Emacs commands for editing as well as a
212 The following keyboard input is supported:
214 .I Home Key, Control-a
215 Goes to the beginning of the line.
217 .I End Key, Control-e
218 Goes to the end of the line.
220 .I Left Arrow Key, Control-b
221 Moves the cursor back one character.
223 .I Right Arrow Key, Control-f
224 Moves the cursor forward one character.
226 .I Up Arrow Key, Control-p
227 Goes back in the history, replaces the current line with the previous
230 .I Down Arrow Key, Control-n
231 Moves forward in the history, replaces the current line with the next
235 Executes the current line if the statement or expression is complete,
236 or waits for further input.
239 Cancel the current line being edited. This will kill any currently
240 in-progress edits or partial editing and go back to a toplevel
244 Deletes the character before the cursor
246 .I Delete Key, Control-d
247 Deletes the character at the current cursor position.
250 Erases the contents of the line until the end of the line and places
251 the result in the cut and paste buffer.
254 Deletes the word starting at the cursor position and appends into the
255 cut and paste buffer. By pressing Alt-d repeatedly, multiple words
256 can be appended into the paste buffer.
259 Pastes the content of the kill buffer at the current cursor position.
262 This is the quote character. It allows the user to enter
263 control-characters that are otherwise taken by the command editing
264 facility. Press Control-Q followed by the character you want to
265 insert, and it will be inserted verbatim into the command line.
268 Terminates the program. This terminates the input for the program.
269 .SH STATIC PROPERTIES AND METHODS
270 Since the methods and properties of the base class from where the
271 statements and expressions are executed are static, they can be
272 invoked directly from the shell. These are the available properties
275 .I void LoadAssembly(string assembly)
276 Loads the given assembly. This is equivalent to passing the compiler
277 the -r: flag with the specified string.
279 .I void LoadPackage(string package)
280 Imports the package specified. This is equivalent to invoking the
281 compiler with the -pkg: flag with the specified string.
283 .I string Prompt { get; set }
284 The prompt used by the shell. It defaults to the value "csharp> ".
285 .I string ContinuationPrompt { get; set; }
286 The prompt used by the shell when further input is required to
287 complete the expression or statement.
290 Displays all the variables that have been defined so far and their
291 types. In the csharp shell declaring new variables will shadow
292 previous variable declarations, this is different than C# when
295 Displays all the using statements in effect.
296 .I TimeSpan Time (Action a)
297 Handy routine to time the time that some code takes to execute. The
298 parameter is an Action delegate, and the return value is a TimeSpan.
302 csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
312 The return value is a TimeSpan, that you can store in a variable for
313 benchmarking purposes.
314 .SH GUI METHODS AND PROPERTIES
315 In addition to the methods and properties available in the console
316 version there are a handful of extra properties available on the GUI
317 version. For example a "PaneContainer" Gtk.Container is exposed that
318 you can use to host Gtk# widgets while prototyping or the "MainWindow"
319 property that gives you access to the current toplevel window.
321 The C# shell will load all the Mono assemblies and C# script files
322 located in the ~/.config/csharp directory on Unix. The assemblies are
323 loaded before the source files are loaded.
325 C# script files are files
326 that have the extension .cs and they should only contain statements
327 and expressions, they can not contain full class definitions (at least
328 not as of Mono 2.0). Full class definitions should be compiled into
329 dlls and stored in that directory.
331 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
332 Martin Baulig, Marek Safar and Raja Harinath. The development was
333 funded by Ximian, Novell and Marek Safar.
335 The Mono Compiler Suite is released under the terms of the GNU GPL or
336 the MIT X11. Please read the accompanying `COPYING' file for details.
337 Alternative licensing for the compiler is available from Novell.
339 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
341 To report bugs in the compiler, you must file them on our bug tracking
343 http://www.mono-project.com/Bugs
345 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
347 The Mono C# compiler was developed by Novell, Inc
348 (http://www.novell.com, http) and is based on the
349 ECMA C# language standard available here:
350 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
352 The home page for the Mono C# compiler is at
353 http://www.mono-project.com/CSharp_Compiler information about the
354 interactive mode for C# is available in http://mono-project.com/CsharpRepl