Small update
[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 \- Interactive C# Shell 
8 .SH SYNOPSIS
9 .B csharp 
10 [options] 
11 .SH DESCRIPTION
12 The 
13 .I csharp
14 is an interactive C# shell that allows the user to enter and evaluate
15 C# statements and expressions from the command line.
16 .PP
17 This command is a shortcut to invoke the 
18 .I gmcs
19 command with the 
20 .I \-\- shell 
21 command line option.   All of the regular compiler options available
22 to gmcs are available in the interactive shell.
23 .SH OPERATION
24 Once you launch the csharp command, you will be greeted with the
25 interactive prompt:
26 .PP
27 .nf
28 $ csharp
29 Mono C# Shell, type "help;" for help
30  
31 Enter statements below.
32 csharp>
33 .fi
34 .PP
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:
39 .PP
40 .nf
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
46 .fi
47 .PP
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:
52 .nf
53 csharp> help;
54 "Static methods:
55   LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
56   [...]
57   ShowVars ();       - Shows defined local variables.
58   ShowUsing ();      - Show active using decltions.
59   help;
60 "
61 csharp>
62 .fi
63 .PP
64 When expressions are entered, the C# shell will display the result of
65 executing the expression:
66 .PP
67 .nf
68 csharp> Math.Sin (Math.PI/4); 
69 0.707106781186547
70 csharp> 1+1;
71 2
72 csharp> "Hello, world".IndexOf (',');
73 5
74 .fi
75 .PP
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:
80 .PP
81 .nf
82 csharp> var a = new XmlDocument ();
83 csharp> a;
84 System.Xml.Document
85 csharp> csharp> a.Name;    
86 "#document"
87 csharp>
88 .fi
89 .PP
90 A few datatypes are handled specially by the C# interactive shell like
91 arrays, System.Collections.Hashtable and are rendered specially
92 instead of just using ToString ():
93 .PP
94 .nf
95 csharp> var pages = new Hashtable () { 
96       >  { "Mono",    "http://www.mono-project.com/" },
97       >  { "Linux",   "http://kernel.org" } };
98 csharp> pages;
99 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
100 .fi
101 .PP
102 .PP
103 It is possible to use LINQ directly in the C# interactive shell since
104 the System.Linq namespace has been imported at startup.   The
105 following sample gets a list of all the files that have not been
106 accessed in a week from /tmp:
107 .PP
108 .nf
109 csharp> using System.IO;
110 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
111 csharp> var old_files = from f in Directory.GetFiles ("/tmp") 
112       >   let fi = new FileInfo (f) 
113       >   where fi.LastAccessTime < LastWeek select f;
114 csharp>
115 .fi
116 .PP
117 You can of course print the results in a single statement as well:
118 .PP
119 .nf
120 csharp> using System.IO;
121 csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
122 csharp> foreach (var old from f in Directory.GetFiles ("/tmp") 
123       >   let fi = new FileInfo (f) 
124       >   where fi.LastAccessTime < LastWeek select f) 
125       >     Console.WriteLine (old);
126 [...]
127 csharp>
128 .fi
129 .PP
130 Unlike compiled C#, the type of a variable can be changed if a new
131 declaration is entered, for example:
132 .PP
133 .nf
134 csharp> var a = 1;
135 csharp> a.GetType ();
136 System.Int32
137 csharp> var a = "Hello";
138 csharp> a.GetType ();
139 System.String
140 csharp> ShowVars ();
141 string a = "Hello"
142 .fi
143 .PP
144 In the case that an expression or a statement is not completed in a
145 single line, a continuation prompt is displayed, for example:
146 .PP
147 .nf
148 csharp> var protocols = new string [] {
149       >    "ftp",
150       >    "http",
151       >    "gopher" 
152       > };
153 csharp> protocols;
154 { "ftp", "http", "gopher" }
155 .fi
156 .PP
157 Long running computations can be interrupted by using the Control-C
158 sequence:
159 .PP
160 .nf
161 csharp> var done = false;
162 csharp> while (!done) { }
163 Interrupted!
164 System.Threading.ThreadAbortException: Thread was being aborted
165   at Class1.Host (System.Object& $retval) [0x00000] 
166   at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000] 
167 csharp>
168 .fi
169 .PP
170 .SH INTERACTIVE EDITING
171 The C# interactive shell contains a line-editor that provides a more
172 advanced command line editing functionality than the operating system
173 provides.     
174 .PP
175 The command set is similar to many other applications (cursor keys)
176 and incorporates some of the Emacs commands for editing as well as a
177 history mechanism to 
178 .PP
179 .PP
180 The following keyboard input is supported:
181 .TP 
182 .I Home Key, Control-a
183 Goes to the beginning of the line.
184 .TP 
185 .I End Key, Control-e
186 Goes to the end of the line.
187 .TP 
188 .I Left Arrow Key, Control-b
189 Moves the cursor back one character.
190 .TP 
191 .I Right Arrow Key, Control-f
192 Moves the cursor forward one character.
193 .TP
194 .I Up Arrow Key, Control-p
195 Goes back in the history, replaces the current line with the previous
196 line in the history.
197 .TP
198 .I Down Arrow Key, Control-n
199 Moves forward in the history, replaces the current line with the next
200 lien in the history.
201 .TP
202 .I Return
203 Executes the current line if the statement or expression is complete,
204 or waits for further input.
205 .TP 
206 .I Control-C
207 Cancel the current line being edited.  This will kill any currently
208 in-progress edits or partial editing and go back to a toplevel
209 definition.
210 .TP
211 .I Backspace Key
212 Deletes the character before the cursor
213 .TP
214 .I Delete Key, Control-d
215 Deletes the character at the current cursor position.
216 .TP
217 .I Control-k
218 Erases the contents of the line until the end of the line and places
219 the result in the cut and paste buffer. 
220 .TP
221 .I Alt-D
222 Deletes the word starting at the cursor position and appends into the
223 cut and paste buffer.    By pressing Alt-d repeatedly, multiple words
224 can be appended into the paste buffer. 
225 .TP
226 .I Control-Y
227 Pastes the content of the kill buffer at the current cursor position. 
228 .TP
229 .I Control-Q
230 This is the quote character.   It allows the user to enter
231 control-characters that are otherwise taken by the command editing
232 facility.   Press Control-Q followed by the character you want to
233 insert, and it will be inserted verbatim into the command line. 
234 .SH STATIC PROPERTIES AND METHODS
235 Since the methods and properties of the base class from where the
236 statements and expressions are executed are static, they can be
237 invoked directly from the shell.   These are the available properties
238 and methods:
239 .TP
240 .I void LoadPackage(string package)
241 Imports the package specified.   This is equivalent to invoking the
242 compiler with the -pkg: flag with the specified string.
243 .TP
244 .I string Prompt { get; set } 
245 The prompt used by the shell.  It defaults to the value "csharp> ".
246 .I string ContinuationPrompt { get; set; } 
247 The prompt used by the shell when further input is required to
248 complete the expression or statement. 
249 .TP 
250 .I void ShowVars()
251 Displays all the variables that have been defined so far and their
252 types.    In the csharp shell declaring new variables will shadow
253 previous variable declarations, this is different than C# when
254 compiled.   
255 .I void ShowUsing()
256 Displays all the using statements in effect.
257 .SH STARTUP FILES
258 The C# shell will load all the C# script files and Mono assemblies
259 located in the ~/.config/csharp directory on Unix.  C# script files
260 are files that have the extension .cs and they should only contain
261 statements and expressions, they can not contain full class
262 definitions (at least not as of Mono 2.0).   Full class definitions
263 should be compiled into dlls and stored in that directory.
264 .SH AUTHORS
265 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
266 Martin Baulig, Marek Safar and Raja Harinath.  The development was
267 funded by Ximian, Novell and Marek Safar.
268 .SH LICENSE
269 The Mono Compiler Suite is released under the terms of the GNU GPL or
270 the MIT X11.  Please read the accompanying `COPYING' file for details.
271 Alternative licensing for the compiler is available from Novell.
272 .SH SEE ALSO
273 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
274 .SH BUGS
275 To report bugs in the compiler, you must file them on our bug tracking
276 system, at:
277 http://www.mono-project.com/Bugs 
278 .SH MAILING LIST
279 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
280 .SH MORE INFORMATION
281 The Mono C# compiler was developed by Novell, Inc
282 (http://www.novell.com, http) and is based on the
283 ECMA C# language standard available here:
284 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
285 .PP
286 The home page for the Mono C# compiler is at
287 http://www.mono-project.com/CSharp_Compiler  information about the
288 interactive mode for C# is available in http://mono-project.com/CsharpRepl