importing messaging-2008 branch to trunk, going on.
[mono.git] / man / csharp.1
1 .de Sp \" Vertical space (when we can't use .PP)
2 .if t .sp .5v
3 .if n .sp
4 ..
5 .TH csharp 1 "4 September 2008"
6 .SH NAME 
7 csharp, gsharp \- Interactive C# Shell 
8 .SH SYNOPSIS
9 .B csharp [--attach PID] 
10 [options] 
11 .P
12 .B gsharp [file1 [file2]]
13 .SH DESCRIPTION
14 The 
15 .I csharp
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 
18 .I mcs
19 command line options can be used in this version of the compiler. 
20 .PP
21 The 
22 .I gsharp
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
28 target process.
29 .PP
30 This version allows a number of scripts to be specified in the command
31 line. 
32 .SH OPTIONS
33 .TP 
34 .I "\-\-attach"
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 
37 .I csharp
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.     
45 .SH OPERATION
46 Once you launch the csharp command, you will be greeted with the
47 interactive prompt:
48 .PP
49 .nf
50 $ csharp
51 Mono C# Shell, type "help;" for help
52  
53 Enter statements below.
54 csharp>
55 .fi
56 .PP
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:
61 .PP
62 .nf
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
68 .fi
69 .PP
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:
74 .nf
75 csharp> help;
76 "Static methods:
77   LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
78   [...]
79   ShowVars ();       - Shows defined local variables.
80   ShowUsing ();      - Show active using decltions.
81   help;
82 "
83 csharp>
84 .fi
85 .PP
86 When expressions are entered, the C# shell will display the result of
87 executing the expression:
88 .PP
89 .nf
90 csharp> Math.Sin (Math.PI/4); 
91 0.707106781186547
92 csharp> 1+1;
93 2
94 csharp> "Hello, world".IndexOf (',');
95 5
96 .fi
97 .PP
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:
102 .PP
103 .nf
104 csharp> var a = new XmlDocument ();
105 csharp> a;
106 System.Xml.Document
107 csharp> csharp> a.Name;    
108 "#document"
109 csharp>
110 .fi
111 .PP
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 ():
116 .PP
117 .nf
118 csharp> var pages = new Hashtable () { 
119       >  { "Mono",    "http://www.mono-project.com/" },
120       >  { "Linux",   "http://kernel.org" } };
121 csharp> pages;
122 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
123 .fi
124 .PP
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:
129 .PP
130 .nf
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;
136 csharp>
137 .fi
138 .PP
139 You can of course print the results in a single statement as well:
140 .PP
141 .nf
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;
147 [...]
148 csharp>
149 .fi
150 .PP
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.
157 .PP
158 If you want to avoid having the IEnumerable rendered at this point,
159 simply assign the value to a variable.
160 .PP
161 Unlike compiled C#, the type of a variable can be changed if a new
162 declaration is entered, for example:
163 .PP
164 .nf
165 csharp> var a = 1;
166 csharp> a.GetType ();
167 System.Int32
168 csharp> var a = "Hello";
169 csharp> a.GetType ();
170 System.String
171 csharp> ShowVars ();
172 string a = "Hello"
173 .fi
174 .PP
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:
177 .PP
178 .nf
179 csharp> var protocols = new string [] {
180       >    "ftp",
181       >    "http",
182       >    "gopher" 
183       > };
184 csharp> protocols;
185 { "ftp", "http", "gopher" }
186 .fi
187 .PP
188 Long running computations can be interrupted by using the Control-C
189 sequence:
190 .PP
191 .nf
192 csharp> var done = false;
193 csharp> while (!done) { }
194 Interrupted!
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] 
198 csharp>
199 .fi
200 .PP
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.
206 .PP
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
209 history mechanism to 
210 .PP
211 .PP
212 The following keyboard input is supported:
213 .TP 
214 .I Home Key, Control-a
215 Goes to the beginning of the line.
216 .TP 
217 .I End Key, Control-e
218 Goes to the end of the line.
219 .TP 
220 .I Left Arrow Key, Control-b
221 Moves the cursor back one character.
222 .TP 
223 .I Right Arrow Key, Control-f
224 Moves the cursor forward one character.
225 .TP
226 .I Up Arrow Key, Control-p
227 Goes back in the history, replaces the current line with the previous
228 line in the history.
229 .TP
230 .I Down Arrow Key, Control-n
231 Moves forward in the history, replaces the current line with the next
232 lien in the history.
233 .TP
234 .I Return
235 Executes the current line if the statement or expression is complete,
236 or waits for further input.
237 .TP 
238 .I Control-C
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
241 definition.
242 .TP
243 .I Backspace Key
244 Deletes the character before the cursor
245 .TP
246 .I Delete Key, Control-d
247 Deletes the character at the current cursor position.
248 .TP
249 .I Control-k
250 Erases the contents of the line until the end of the line and places
251 the result in the cut and paste buffer. 
252 .TP
253 .I Alt-D
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. 
257 .TP
258 .I Control-Y
259 Pastes the content of the kill buffer at the current cursor position. 
260 .TP
261 .I Control-Q
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. 
266 .TP
267 .I Control-D
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
273 and methods:
274 .TP
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. 
278 .TP
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.
282 .TP
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. 
288 .TP 
289 .I void ShowVars()
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
293 compiled.   
294 .I void ShowUsing()
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.
299 For example:
300 .PP
301 .nf
302 csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
303 0
304 1
305 2
306 3
307 4
308 00:00:00.0043230
309 csharp>
310 .fi
311 .PP
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. 
320 .SH STARTUP FILES
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. 
324 .PP
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.
330 .SH AUTHORS
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.
334 .SH LICENSE
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.
338 .SH SEE ALSO
339 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
340 .SH BUGS
341 To report bugs in the compiler, you must file them on our bug tracking
342 system, at:
343 http://www.mono-project.com/Bugs 
344 .SH MAILING LIST
345 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
346 .SH MORE INFORMATION
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
351 .PP
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