1csharp(1) General Commands Manual csharp(1)
2
3
4
6 csharp, gsharp - Interactive C# Shell
7
9 csharp [--attach PID] [file1 [file2]] [options]
10
11 gsharp [file1 [file2]]
12
14 The csharp is an interactive C# shell that allows the user to enter and
15 evaluate C# statements and expressions from the command line. The
16 regular mcs command line options can be used in this version of the
17 compiler.
18
19 The gsharp command is a GUI version of the C# interpreter that uses
20 Gtk# and provides an area to attach widgets as well. This version
21 can be attached to other Gtk# applications in a safe way as it injects
22 itself into the main loop of a Gtk# application, avoiding any problems
23 arising from the multi-threaded nature of injecting itself into a tar‐
24 get process.
25
26 Files specified in the command line will be loaded and executed as
27 scripts.
28
30 --attach
31 This is an advanced option and should only be used if you have a
32 deep understanding of multi-threading. This option is avail‐
33 ble on the csharp command and allows the compiler to be injected
34 into other processes. This is done by injecting the C# shell in
35 a separate thread that runs concurrently with your application.
36 This means that you must take special measures to avoid crashing
37 the target application while using it. For example, you might
38 have to take the proper locks before issuing any commands that
39 might affect the target process state, or sending commands
40 through a method dispatcher.
41
43 Once you launch the csharp command, you will be greeted with the inter‐
44 active prompt:
45
46 $ csharp
47 Mono C# Shell, type "help;" for help
48
49 Enter statements below.
50 csharp>
51
52 A number of namespaces are pre-defined with C# these include System,
53 System.Linq, System.Collections and System.Collections.Generic. Unlike
54 the compiled mode, it is possible to add new using statements as you
55 type code, for example:
56
57 csharp> new XmlDocument ();
58 <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?
59 csharp> using System.Xml;
60 csharp> new XmlDocument ();
61 System.Xml.XmlDocument
62
63 Every time a command is typed, the scope of that command is one of a
64 class that derives from the class Mono.CSharp.InteractiveBase. This
65 class defines a number of static properties and methods. To display a
66 list of available commands access the `help' property:
67 csharp> help;
68 "Static methods:
69 LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
70 [...]
71 ShowVars (); - Shows defined local variables.
72 ShowUsing (); - Show active using decltions.
73 help;
74 "
75 csharp>
76
77 When expressions are entered, the C# shell will display the result of
78 executing the expression:
79
80 csharp> Math.Sin (Math.PI/4);
81 0.707106781186547
82 csharp> 1+1;
83 2
84 csharp> "Hello, world".IndexOf (',');
85 5
86
87 The C# shell uses the ToString() method on the returned object to dis‐
88 play the object, this sometimes can be limiting since objects that do
89 not override the ToString() method will get the default behavior from
90 System.Object which is merely to display their type name:
91
92 csharp> var a = new XmlDocument ();
93 csharp> a;
94 System.Xml.Document
95 csharp> csharp> a.Name;
96 "#document"
97 csharp>
98
99 A few datatypes are handled specially by the C# interactive shell like
100 arrays, System.Collections.Hashtable, objects that implement Sys‐
101 tem.Collections.IEnumerable and IDictionary and are rendered specially
102 instead of just using ToString ():
103
104 csharp> var pages = new Hashtable () {
105 > { "Mono", "http://www.mono-project.com/" },
106 > { "Linux", "http://kernel.org" } };
107 csharp> pages;
108 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
109
110 It is possible to use LINQ directly in the C# interactive shell since
111 the System.Linq namespace has been imported at startup. The following
112 sample gets a list of all the files that have not been accessed in a
113 week from /tmp:
114
115 csharp> using System.IO;
116 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
117 csharp> var old_files = from f in Directory.GetFiles ("/tmp")
118 > let fi = new FileInfo (f)
119 > where fi.LastAccessTime < LastWeek select f;
120 csharp>
121
122 You can of course print the results in a single statement as well:
123
124 csharp> using System.IO;
125 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
126 csharp> from f in Directory.GetFiles ("/tmp")
127 > let fi = new FileInfo (f)
128 > where fi.LastAccessTime < last_week select f;
129 [...]
130 csharp>
131
132 LINQ and its functional foundation produce on-demand code for IEnumer‐
133 able return values. For instance, the return value from a using `from'
134 is an IEnumerable that is evaluated on demand. The automatic render‐
135 ing of IEnumerables on the command line will trigger the IEnumerable
136 pipeline to execute at that point instead of having its execution
137 delayed until a later point.
138
139 If you want to avoid having the IEnumerable rendered at this point,
140 simply assign the value to a variable.
141
142 Unlike compiled C#, the type of a variable can be changed if a new dec‐
143 laration is entered, for example:
144
145 csharp> var a = 1;
146 csharp> a.GetType ();
147 System.Int32
148 csharp> var a = "Hello";
149 csharp> a.GetType ();
150 System.String
151 csharp> ShowVars ();
152 string a = "Hello"
153
154 In the case that an expression or a statement is not completed in a
155 single line, a continuation prompt is displayed, for example:
156
157 csharp> var protocols = new string [] {
158 > "ftp",
159 > "http",
160 > "gopher"
161 > };
162 csharp> protocols;
163 { "ftp", "http", "gopher" }
164
165 Long running computations can be interrupted by using the Control-C
166 sequence:
167
168 csharp> var done = false;
169 csharp> while (!done) { }
170 Interrupted!
171 System.Threading.ThreadAbortException: Thread was being aborted
172 at Class1.Host (System.Object& $retval) [0x00000]
173 at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
174 csharp>
175
177 The C# interactive shell contains a line-editor that provides a more
178 advanced command line editing functionality than the operating system
179 provides. These are available in the command line version, the GUI
180 versions uses the standard Gtk# key bindings.
181
182 The command set is similar to many other applications (cursor keys) and
183 incorporates some of the Emacs commands for editing as well as a his‐
184 tory mechanism to
185
186 The following keyboard input is supported:
187
188 Home Key, Control-a
189 Goes to the beginning of the line.
190
191 End Key, Control-e
192 Goes to the end of the line.
193
194 Left Arrow Key, Control-b
195 Moves the cursor back one character.
196
197 Right Arrow Key, Control-f
198 Moves the cursor forward one character.
199
200 Up Arrow Key, Control-p
201 Goes back in the history, replaces the current line with the
202 previous line in the history.
203
204 Down Arrow Key, Control-n
205 Moves forward in the history, replaces the current line with the
206 next lien in the history.
207
208 Return Executes the current line if the statement or expression is com‐
209 plete, or waits for further input.
210
211 Control-C
212 Cancel the current line being edited. This will kill any cur‐
213 rently in-progress edits or partial editing and go back to a
214 toplevel definition.
215
216 Backspace Key
217 Deletes the character before the cursor
218
219 Delete Key, Control-d
220 Deletes the character at the current cursor position.
221
222 Control-k
223 Erases the contents of the line until the end of the line and
224 places the result in the cut and paste buffer.
225
226 Alt-D Deletes the word starting at the cursor position and appends
227 into the cut and paste buffer. By pressing Alt-d repeatedly,
228 multiple words can be appended into the paste buffer.
229
230 Control-Y
231 Pastes the content of the kill buffer at the current cursor
232 position.
233
234 Control-Q
235 This is the quote character. It allows the user to enter con‐
236 trol-characters that are otherwise taken by the command editing
237 facility. Press Control-Q followed by the character you want
238 to insert, and it will be inserted verbatim into the command
239 line.
240
241 Control-D
242 Terminates the program. This terminates the input for the pro‐
243 gram.
244
246 Since the methods and properties of the base class from where the
247 statements and expressions are executed are static, they can be invoked
248 directly from the shell. These are the available properties and meth‐
249 ods:
250
251 void LoadAssembly(string assembly)
252 Loads the given assembly. This is equivalent to passing the
253 compiler the -r: flag with the specified string.
254
255 void LoadPackage(string package)
256 Imports the package specified. This is equivalent to invoking
257 the compiler with the -pkg: flag with the specified string.
258
259 string Prompt { get; set }
260 The prompt used by the shell. It defaults to the value "csharp>
261 ". string ContinuationPrompt { get; set; } The prompt used by
262 the shell when further input is required to complete the expres‐
263 sion or statement.
264
265 void ShowVars()
266 Displays all the variables that have been defined so far and
267 their types. In the csharp shell declaring new variables will
268 shadow previous variable declarations, this is different than C#
269 when compiled. void ShowUsing() Displays all the using state‐
270 ments in effect. TimeSpan Time (Action a) Handy routine to time
271 the time that some code takes to execute. The parameter is an
272 Action delegate, and the return value is a TimeSpan. For exam‐
273 ple:
274
275 csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
276 0
277 1
278 2
279 3
280 4
281 00:00:00.0043230
282 csharp>
283
284 The return value is a TimeSpan, that you can store in a variable for
285 benchmarking purposes.
286
288 In addition to the methods and properties available in the console ver‐
289 sion there are a handful of extra properties available on the GUI ver‐
290 sion. For example a "PaneContainer" Gtk.Container is exposed that you
291 can use to host Gtk# widgets while prototyping or the "MainWindow"
292 property that gives you access to the current toplevel window.
293
295 The C# shell will load all the Mono assemblies and C# script files
296 located in the ~/.config/csharp directory on Unix. The assemblies are
297 loaded before the source files are loaded.
298
299 C# script files are files that have the extension .cs and they should
300 only contain statements and expressions, they can not contain full
301 class definitions (at least not as of Mono 2.0). Full class defini‐
302 tions should be compiled into dlls and stored in that directory.
303
305 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap, Mar‐
306 tin Baulig, Marek Safar and Raja Harinath. The development was funded
307 by Ximian, Novell and Marek Safar.
308
310 The Mono Compiler Suite is released under the terms of the GNU GPL or
311 the MIT X11. Please read the accompanying `COPYING' file for details.
312 Alternative licensing for the compiler is available from Novell.
313
315 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
316
318 To report bugs in the compiler, you must file them on our bug tracking
319 system, at: http://www.mono-project.com/Bugs
320
322 The Mono Mailing lists are listed at http://www.mono-project.com/Mail‐
323 ing_Lists
324
326 The Mono C# compiler was developed by Novell, Inc (http://www.nov‐
327 ell.com, http) and is based on the ECMA C# language standard available
328 here: http://www.ecma.ch/ecma1/STAND/ecma-334.htm
329
330 The home page for the Mono C# compiler is at http://www.mono-
331 project.com/CSharp_Compiler information about the interactive mode for
332 C# is available in http://mono-project.com/CsharpRepl
333
334
335
336 4 September 2008 csharp(1)