OneMore
[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, objects that implement
92 System.Collections.IEnumerable and IDictionary and are rendered
93 specially instead of just using ToString ():
94 .PP
95 .nf
96 csharp> var pages = new Hashtable () { 
97       >  { "Mono",    "http://www.mono-project.com/" },
98       >  { "Linux",   "http://kernel.org" } };
99 csharp> pages;
100 {{ "Mono", "http://www.mono-project.com/" }, { "Linux", "http://kernel.org" }}
101 .fi
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> from f in Directory.GetFiles ("/tmp") 
123       >   let fi = new FileInfo (f) 
124       >   where fi.LastAccessTime < last_week select f;
125 [...]
126 csharp>
127 .fi
128 .PP
129 LINQ and its functional foundation produce on-demand code for
130 IEnumerable return values.  For instance, the return value from a
131 using `from' is an IEnumerable that is evaluated on demand.   The
132 automatic rendering of IEnumerables on the command line will trigger
133 the IEnumerable pipeline to execute at that point instead of having
134 its execution delayed until a later point.
135 .PP
136 If you want to avoid having the IEnumerable rendered at this point,
137 simply assign the value to a variable.
138 .PP
139 Unlike compiled C#, the type of a variable can be changed if a new
140 declaration is entered, for example:
141 .PP
142 .nf
143 csharp> var a = 1;
144 csharp> a.GetType ();
145 System.Int32
146 csharp> var a = "Hello";
147 csharp> a.GetType ();
148 System.String
149 csharp> ShowVars ();
150 string a = "Hello"
151 .fi
152 .PP
153 In the case that an expression or a statement is not completed in a
154 single line, a continuation prompt is displayed, for example:
155 .PP
156 .nf
157 csharp> var protocols = new string [] {
158       >    "ftp",
159       >    "http",
160       >    "gopher" 
161       > };
162 csharp> protocols;
163 { "ftp", "http", "gopher" }
164 .fi
165 .PP
166 Long running computations can be interrupted by using the Control-C
167 sequence:
168 .PP
169 .nf
170 csharp> var done = false;
171 csharp> while (!done) { }
172 Interrupted!
173 System.Threading.ThreadAbortException: Thread was being aborted
174   at Class1.Host (System.Object& $retval) [0x00000] 
175   at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000] 
176 csharp>
177 .fi
178 .PP
179 .SH INTERACTIVE EDITING
180 The C# interactive shell contains a line-editor that provides a more
181 advanced command line editing functionality than the operating system
182 provides.     
183 .PP
184 The command set is similar to many other applications (cursor keys)
185 and incorporates some of the Emacs commands for editing as well as a
186 history mechanism to 
187 .PP
188 .PP
189 The following keyboard input is supported:
190 .TP 
191 .I Home Key, Control-a
192 Goes to the beginning of the line.
193 .TP 
194 .I End Key, Control-e
195 Goes to the end of the line.
196 .TP 
197 .I Left Arrow Key, Control-b
198 Moves the cursor back one character.
199 .TP 
200 .I Right Arrow Key, Control-f
201 Moves the cursor forward one character.
202 .TP
203 .I Up Arrow Key, Control-p
204 Goes back in the history, replaces the current line with the previous
205 line in the history.
206 .TP
207 .I Down Arrow Key, Control-n
208 Moves forward in the history, replaces the current line with the next
209 lien in the history.
210 .TP
211 .I Return
212 Executes the current line if the statement or expression is complete,
213 or waits for further input.
214 .TP 
215 .I Control-C
216 Cancel the current line being edited.  This will kill any currently
217 in-progress edits or partial editing and go back to a toplevel
218 definition.
219 .TP
220 .I Backspace Key
221 Deletes the character before the cursor
222 .TP
223 .I Delete Key, Control-d
224 Deletes the character at the current cursor position.
225 .TP
226 .I Control-k
227 Erases the contents of the line until the end of the line and places
228 the result in the cut and paste buffer. 
229 .TP
230 .I Alt-D
231 Deletes the word starting at the cursor position and appends into the
232 cut and paste buffer.    By pressing Alt-d repeatedly, multiple words
233 can be appended into the paste buffer. 
234 .TP
235 .I Control-Y
236 Pastes the content of the kill buffer at the current cursor position. 
237 .TP
238 .I Control-Q
239 This is the quote character.   It allows the user to enter
240 control-characters that are otherwise taken by the command editing
241 facility.   Press Control-Q followed by the character you want to
242 insert, and it will be inserted verbatim into the command line. 
243 .TP
244 .I Control-D
245 Terminates the program.   This terminates the input for the program.
246 .SH STATIC PROPERTIES AND METHODS
247 Since the methods and properties of the base class from where the
248 statements and expressions are executed are static, they can be
249 invoked directly from the shell.   These are the available properties
250 and methods:
251 .TP
252 .I void LoadPackage(string package)
253 Imports the package specified.   This is equivalent to invoking the
254 compiler with the -pkg: flag with the specified string.
255 .TP
256 .I string Prompt { get; set } 
257 The prompt used by the shell.  It defaults to the value "csharp> ".
258 .I string ContinuationPrompt { get; set; } 
259 The prompt used by the shell when further input is required to
260 complete the expression or statement. 
261 .TP 
262 .I void ShowVars()
263 Displays all the variables that have been defined so far and their
264 types.    In the csharp shell declaring new variables will shadow
265 previous variable declarations, this is different than C# when
266 compiled.   
267 .I void ShowUsing()
268 Displays all the using statements in effect.
269 .SH STARTUP FILES
270 The C# shell will load all the C# script files and Mono assemblies
271 located in the ~/.config/csharp directory on Unix.  C# script files
272 are files that have the extension .cs and they should only contain
273 statements and expressions, they can not contain full class
274 definitions (at least not as of Mono 2.0).   Full class definitions
275 should be compiled into dlls and stored in that directory.
276 .SH AUTHORS
277 The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,
278 Martin Baulig, Marek Safar and Raja Harinath.  The development was
279 funded by Ximian, Novell and Marek Safar.
280 .SH LICENSE
281 The Mono Compiler Suite is released under the terms of the GNU GPL or
282 the MIT X11.  Please read the accompanying `COPYING' file for details.
283 Alternative licensing for the compiler is available from Novell.
284 .SH SEE ALSO
285 gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
286 .SH BUGS
287 To report bugs in the compiler, you must file them on our bug tracking
288 system, at:
289 http://www.mono-project.com/Bugs 
290 .SH MAILING LIST
291 The Mono Mailing lists are listed at http://www.mono-project.com/Mailing_Lists
292 .SH MORE INFORMATION
293 The Mono C# compiler was developed by Novell, Inc
294 (http://www.novell.com, http) and is based on the
295 ECMA C# language standard available here:
296 http://www.ecma.ch/ecma1/STAND/ecma-334.htm
297 .PP
298 The home page for the Mono C# compiler is at
299 http://www.mono-project.com/CSharp_Compiler  information about the
300 interactive mode for C# is available in http://mono-project.com/CsharpRepl