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

NAME

6       csharp - Interactive C# Shell and Scripting
7

SYNOPSIS

9       csharp  [--attach  PID]  [-e  EXPRESSION]  [file1  [file2]]  [compiler-
10       options] [--|-s script-options]
11

DESCRIPTION

13       The csharp command is an interactive C# shell and scripting  host  that
14       allows  the  user  to  enter and evaluate C# statements and expressions
15       from the command line or execute C# scripts.  The regular  mcs  command
16       line options can be used in this version of the compiler.
17
18       Files  specified  in  the  command  line will be loaded and executed as
19       scripts.
20
21       Starting with Mono 2.10, the csharp command can be used  as  an  inter‐
22       preter executed by executables flagged with the Unix execute attribute.
23       To do this, make the first line of your C# source code look like this:
24
25         #!/usr/bin/csharp
26         Console.WriteLine ("Hello, World");
27
28       Starting with Mono 5.0, command line arguments may now be passed to the
29       csharp command by specifying either the -s or -- (script) options.
30
31       The -s option is ideal for interpreting executable scripts that utilize
32       shebang syntax (introduced in Mono  2.10).  This  allows  command  line
33       arguments to be passed to and consumed cleanly by the script:
34
35         #!/usr/bin/csharp -s
36         foreach (var arg in Args)
37           Console.WriteLine ($"script argument: {arg}");
38

OPTIONS

40       The  commands  accept all of the commands that are available to the mcs
41       command, so you can reference assemblies, specify paths, language level
42       and  so  on from the command line.   In addition, the following command
43       line options are supported:
44
45       -s SCRIPT_FILE
46              This option is ideal for authoring executable scripts that  uti‐
47              lize the Unix shebang feature. Unix will implicitly append as an
48              argument the path of the script to execute. When the  executable
49              is invoked, any arguments then passed to it will be available in
50              the Args global. Example: #!/usr/bin/env csharp -s
51
52       --     Any arguments that follow will not be  passed  to  the  compiler
53              driver,  and  instead will be made available in the Args global.
54              Example: csharp -- a b c will result in Args = { "a", "b", "c" }
55              in the interactive shell.
56
57       --attach
58              This is an advanced option and should only be used if you have a
59              deep understanding of multi-threading.     This option is avail‐
60              ble on the csharp command and allows the compiler to be injected
61              into other processes.  This is done by injecting the C# shell in
62              a  separate thread that runs concurrently with your application.
63              This means that you must take special measures to avoid crashing
64              the  target  application while using it.  For example, you might
65              have to take the proper locks before issuing any  commands  that
66              might  affect  the  target  process  state,  or sending commands
67              through a method dispatcher.
68
69       -e EXPRESSION
70              This will evaluate the specified C# EXPRESSION and exit
71

OPERATION

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

INTERACTIVE EDITING

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

STATIC PROPERTIES AND METHODS

276       Since  the  methods  and  properties  of  the base class from where the
277       statements and expressions are executed are static, they can be invoked
278       directly from the shell.   These are the available properties and meth‐
279       ods:
280
281       Args   An easy to consume array of any arguments specified after either
282              -s  or  -- on the command line. Ideal for self-executing scripts
283              utilizing the -s option.
284
285       void LoadAssembly(string assembly)
286              Loads the given assembly.   This is equivalent  to  passing  the
287              compiler the -r: flag with the specified string.
288
289       void LoadPackage(string package)
290              Imports  the package specified.   This is equivalent to invoking
291              the compiler with the -pkg: flag with the specified string.
292
293       string Prompt { get; set }
294              The prompt used by the shell.  It defaults to the value "csharp>
295              ".   string  ContinuationPrompt { get; set; } The prompt used by
296              the shell when further input is required to complete the expres‐
297              sion or statement.
298
299       void ShowVars()
300              Displays  all  the  variables  that have been defined so far and
301              their types.    In the csharp shell declaring new variables will
302              shadow previous variable declarations, this is different than C#
303              when compiled.  void ShowUsing() Displays all the  using  state‐
304              ments in effect.  TimeSpan Time (Action a) Handy routine to time
305              the time that some code takes to execute.   The parameter is  an
306              Action  delegate, and the return value is a TimeSpan.  For exam‐
307              ple:
308
309       csharp> Time (() => { for (int i = 0; i < 5; i++) Console.WriteLine (i);});
310       0
311       1
312       2
313       3
314       4
315       00:00:00.0043230
316       csharp>
317
318       The return value is a TimeSpan, that you can store in  a  variable  for
319       benchmarking purposes.
320

GUI METHODS AND PROPERTIES

322       In addition to the methods and properties available in the console ver‐
323       sion there are a handful of extra properties available on the GUI  ver‐
324       sion.   For example a "PaneContainer" Gtk.Container is exposed that you
325       can use to host Gtk# widgets  while  prototyping  or  the  "MainWindow"
326       property that gives you access to the current toplevel window.
327

STARTUP FILES

329       The  C#  shell  will  load  all the Mono assemblies and C# script files
330       located in the ~/.config/csharp directory on Unix.  The assemblies  are
331       loaded before the source files are loaded.
332
333       C#  script  files are files that have the extension .cs and they should
334       only contain statements and expressions,  they  can  not  contain  full
335       class  definitions  (at  least not as of Mono 2.0).  Full class defini‐
336       tions should be compiled into dlls and stored in that directory.
337

AUTHORS

339       The Mono C# Compiler was written by Miguel de Icaza, Ravi Pratap,  Mar‐
340       tin  Baulig, Marek Safar and Raja Harinath.  The development was funded
341       by Ximian, Novell and Marek Safar.
342

LICENSE

344       The Mono Compiler Suite is released under the terms of the GNU  GPL  or
345       the  MIT X11.  Please read the accompanying `COPYING' file for details.
346       Alternative licensing for the compiler is available from Novell.
347

SEE ALSO

349       gmcs(1), mcs(1), mdb(1), mono(1), pkg-config(1)
350

BUGS

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

MAILING LIST

356       The Mono Mailing lists are listed at http://www.mono-project.com/commu
357       nity/help/mailing-lists/
358

MORE INFORMATION

360       The Mono C# compiler was  developed  by  Novell,  Inc  (http://www.nov
361       ell.com,  http) and is based on the ECMA C# language standard available
362       here: http://www.ecma.ch/ecma1/STAND/ecma-334.htm
363
364       The  home  page  for  the  Mono  C#  compiler  is  at  http://www.mono-
365       project.com/docs/about-mono/languages/csharp/   information  about  the
366       interactive   mode   for    C#    is    available    in    http://mono-
367       project.com/docs/tools+libraries/tools/repl/
368
369
370
371                                 22 March 2017                       csharp(1)
Impressum