1csharp(1)                   General Commands Manual                  csharp(1)
2
3
4

NAME

6       csharp, gsharp - Interactive C# Shell
7

SYNOPSIS

9       csharp [--attach PID] [-e EXPRESSION] [file1 [file2]] [options]
10
11       gsharp [file1 [file2]]
12

DESCRIPTION

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
29       Starting with Mono 2.10, the csharp command can be used  as  an  inter‐
30       preter executed by executables flagged with the Unix execute attribute.
31       To do this, make the first line of your C# source code look like this:
32       "#!/usr/bin/csharp"
33       Console.WriteLine ("Hello, World");
34

OPTIONS

36       The commands accept all of the commands that are available to  the  mcs
37       command, so you can reference assemblies, specify paths, language level
38       and so on from the command line.   In addition, the  following  command
39       line options are supported:
40
41       --attach
42              This is an advanced option and should only be used if you have a
43              deep understanding of multi-threading.     This option is avail‐
44              ble on the csharp command and allows the compiler to be injected
45              into other processes.  This is done by injecting the C# shell in
46              a  separate thread that runs concurrently with your application.
47              This means that you must take special measures to avoid crashing
48              the  target  application while using it.  For example, you might
49              have to take the proper locks before issuing any  commands  that
50              might  affect  the  target  process  state,  or sending commands
51              through a method dispatcher.
52
53       -e EXPRESSION
54              This will evaluate the specified C# EXPRESSION and exit
55

OPERATION

57       Once you launch the csharp command, you will be greeted with the inter‐
58       active prompt:
59
60       $ csharp
61       Mono C# Shell, type "help;" for help
62
63       Enter statements below.
64       csharp>
65
66       A  number  of  namespaces are pre-defined with C# these include System,
67       System.Linq, System.Collections and System.Collections.Generic.  Unlike
68       the  compiled  mode,  it is possible to add new using statements as you
69       type code, for example:
70
71       csharp> new XmlDocument ();
72       <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?
73       csharp> using System.Xml;
74       csharp> new XmlDocument ();
75       System.Xml.XmlDocument
76
77       Every time a command is typed, the scope of that command is  one  of  a
78       class  that  derives from the class Mono.CSharp.InteractiveBase.   This
79       class defines a number of static properties and methods.   To display a
80       list of available commands access the `help' property:
81       csharp> help;
82       "Static methods:
83         LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)
84         [...]
85         ShowVars ();       - Shows defined local variables.
86         ShowUsing ();      - Show active using decltions.
87         help;
88       "
89       csharp>
90
91       When  expressions  are entered, the C# shell will display the result of
92       executing the expression:
93
94       csharp> Math.Sin (Math.PI/4);
95       0.707106781186547
96       csharp> 1+1;
97       2
98       csharp> "Hello, world".IndexOf (',');
99       5
100
101       The C# shell uses the ToString() method on the returned object to  dis‐
102       play  the  object, this sometimes can be limiting since objects that do
103       not override the ToString() method will get the default  behavior  from
104       System.Object which is merely to display their type name:
105
106       csharp> var a = new XmlDocument ();
107       csharp> a;
108       System.Xml.Document
109       csharp> csharp> a.Name;
110       "#document"
111       csharp>
112
113       A  few datatypes are handled specially by the C# interactive shell like
114       arrays,  System.Collections.Hashtable,  objects  that  implement   Sys‐
115       tem.Collections.IEnumerable  and IDictionary and are rendered specially
116       instead of just using ToString ():
117
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
124       It is possible to use LINQ directly in the C# interactive  shell  since
125       the System.Linq namespace has been imported at startup.   The following
126       sample gets a list of all the files that have not been  accessed  in  a
127       week from /tmp:
128
129       csharp> using System.IO;
130       csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
131       csharp> var old_files = from f in Directory.GetFiles ("/tmp")
132             >   let fi = new FileInfo (f)
133             >   where fi.LastAccessTime < LastWeek select f;
134       csharp>
135
136       You can of course print the results in a single statement as well:
137
138       csharp> using System.IO;
139       csharp> var last_week = DateTime.Now - TimeSpan.FromDays (7);
140       csharp> from f in Directory.GetFiles ("/tmp")
141             >   let fi = new FileInfo (f)
142             >   where fi.LastAccessTime < last_week select f;
143       [...]
144       csharp>
145
146       LINQ  and its functional foundation produce on-demand code for IEnumer‐
147       able return values.  For instance, the return value from a using `from'
148       is  an IEnumerable that is evaluated on demand.   The automatic render‐
149       ing of IEnumerables on the command line will  trigger  the  IEnumerable
150       pipeline  to  execute  at  that  point  instead of having its execution
151       delayed until a later point.
152
153       If you want to avoid having the IEnumerable  rendered  at  this  point,
154       simply assign the value to a variable.
155
156       Unlike compiled C#, the type of a variable can be changed if a new dec‐
157       laration is entered, for example:
158
159       csharp> var a = 1;
160       csharp> a.GetType ();
161       System.Int32
162       csharp> var a = "Hello";
163       csharp> a.GetType ();
164       System.String
165       csharp> ShowVars ();
166       string a = "Hello"
167
168       In the case that an expression or a statement is  not  completed  in  a
169       single line, a continuation prompt is displayed, for example:
170
171       csharp> var protocols = new string [] {
172             >    "ftp",
173             >    "http",
174             >    "gopher"
175             > };
176       csharp> protocols;
177       { "ftp", "http", "gopher" }
178
179       Long  running  computations  can  be interrupted by using the Control-C
180       sequence:
181
182       csharp> var done = false;
183       csharp> while (!done) { }
184       Interrupted!
185       System.Threading.ThreadAbortException: Thread was being aborted
186         at Class1.Host (System.Object& $retval) [0x00000]
187         at Mono.CSharp.InteractiveShell.ExecuteBlock (Mono.CSharp.Class host, Mono.CSharp.Undo undo) [0x00000]
188       csharp>
189

INTERACTIVE EDITING

191       The C# interactive shell contains a line-editor that  provides  a  more
192       advanced  command  line editing functionality than the operating system
193       provides.   These are available in the command line  version,  the  GUI
194       versions uses the standard Gtk# key bindings.
195
196       The command set is similar to many other applications (cursor keys) and
197       incorporates some of the Emacs commands for editing as well as  a  his‐
198       tory mechanism to
199
200       The following keyboard input is supported:
201
202       Home Key, Control-a
203              Goes to the beginning of the line.
204
205       End Key, Control-e
206              Goes to the end of the line.
207
208       Left Arrow Key, Control-b
209              Moves the cursor back one character.
210
211       Right Arrow Key, Control-f
212              Moves the cursor forward one character.
213
214       Up Arrow Key, Control-p
215              Goes  back  in  the  history, replaces the current line with the
216              previous line in the history.
217
218       Down Arrow Key, Control-n
219              Moves forward in the history, replaces the current line with the
220              next lien in the history.
221
222       Return Executes the current line if the statement or expression is com‐
223              plete, or waits for further input.
224
225       Control-C
226              Cancel the current line being edited.  This will kill  any  cur‐
227              rently  in-progress  edits  or  partial editing and go back to a
228              toplevel definition.
229
230       Backspace Key
231              Deletes the character before the cursor
232
233       Delete Key, Control-d
234              Deletes the character at the current cursor position.
235
236       Control-k
237              Erases the contents of the line until the end of  the  line  and
238              places the result in the cut and paste buffer.
239
240       Alt-D  Deletes  the  word  starting  at the cursor position and appends
241              into the cut and paste buffer.    By pressing Alt-d  repeatedly,
242              multiple words can be appended into the paste buffer.
243
244       Control-Y
245              Pastes  the  content  of  the  kill buffer at the current cursor
246              position.
247
248       Control-Q
249              This is the quote character.   It allows the user to enter  con‐
250              trol-characters  that are otherwise taken by the command editing
251              facility.   Press Control-Q followed by the character  you  want
252              to  insert,  and  it  will be inserted verbatim into the command
253              line.
254
255       Control-D
256              Terminates the program.   This terminates the input for the pro‐
257              gram.
258

STATIC PROPERTIES AND METHODS

260       Since  the  methods  and  properties  of  the base class from where the
261       statements and expressions are executed are static, they can be invoked
262       directly from the shell.   These are the available properties and meth‐
263       ods:
264
265       void LoadAssembly(string assembly)
266              Loads the given assembly.   This is equivalent  to  passing  the
267              compiler the -r: flag with the specified string.
268
269       void LoadPackage(string package)
270              Imports  the package specified.   This is equivalent to invoking
271              the compiler with the -pkg: flag with the specified string.
272
273       string Prompt { get; set }
274              The prompt used by the shell.  It defaults to the value "csharp>
275              ".   string  ContinuationPrompt { get; set; } The prompt used by
276              the shell when further input is required to complete the expres‐
277              sion or statement.
278
279       void ShowVars()
280              Displays  all  the  variables  that have been defined so far and
281              their types.    In the csharp shell declaring new variables will
282              shadow previous variable declarations, this is different than C#
283              when compiled.  void ShowUsing() Displays all the  using  state‐
284              ments in effect.  TimeSpan Time (Action a) Handy routine to time
285              the time that some code takes to execute.   The parameter is  an
286              Action  delegate, and the return value is a TimeSpan.  For exam‐
287              ple:
288
289       csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
290       0
291       1
292       2
293       3
294       4
295       00:00:00.0043230
296       csharp>
297
298       The return value is a TimeSpan, that you can store in  a  variable  for
299       benchmarking purposes.
300

GUI METHODS AND PROPERTIES

302       In addition to the methods and properties available in the console ver‐
303       sion there are a handful of extra properties available on the GUI  ver‐
304       sion.   For example a "PaneContainer" Gtk.Container is exposed that you
305       can use to host Gtk# widgets  while  prototyping  or  the  "MainWindow"
306       property that gives you access to the current toplevel window.
307

STARTUP FILES

309       The  C#  shell  will  load  all the Mono assemblies and C# script files
310       located in the ~/.config/csharp directory on Unix.  The assemblies  are
311       loaded before the source files are loaded.
312
313       C#  script  files are files that have the extension .cs and they should
314       only contain statements and expressions,  they  can  not  contain  full
315       class  definitions  (at  least not as of Mono 2.0).  Full class defini‐
316       tions should be compiled into dlls and stored in that directory.
317

AUTHORS

319       The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,  Mar‐
320       tin  Baulig, Marek Safar and Raja Harinath.  The development was funded
321       by Ximian, Novell and Marek Safar.
322

LICENSE

324       The Mono Compiler Suite is released under the terms of the GNU  GPL  or
325       the  MIT X11.  Please read the accompanying `COPYING' file for details.
326       Alternative licensing for the compiler is available from Novell.
327

SEE ALSO

329       gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
330

BUGS

332       To report bugs in the compiler, you must file them on our bug  tracking
333       system, at: http://www.mono-project.com/community/bugs/
334

MAILING LIST

336       The Mono Mailing lists are listed at http://www.mono-project.com/commu
337       nity/help/mailing-lists/
338

MORE INFORMATION

340       The Mono C# compiler was  developed  by  Novell,  Inc  (http://www.nov
341       ell.com,  http) and is based on the ECMA C# language standard available
342       here: http://www.ecma.ch/ecma1/STAND/ecma-334.htm
343
344       The  home  page  for  the  Mono  C#  compiler  is  at  http://www.mono-
345       project.com/docs/about-mono/languages/csharp/   information  about  the
346       interactive   mode   for    C#    is    available    in    http://mono-
347       project.com/docs/tools+libraries/tools/repl/
348
349
350
351                               4 September 2008                      csharp(1)
Impressum