C# REPL and Interactive interpreter

bitbucket_2This blog post introduces CSRepl, the C# REPL and interactive interpreter which I have founded on BitBucket. REPL stands for Read-eval-print loop and is a simple, interactive computer programming environment. It can be used to evaluate C# expressions and statements on the fly as well as creating types (classes, stucts, enums).

CSRepl can conveniently be used to quickly explore .NET Framework classes and APIs as well as third party assemblies.

Download

Download

Download installer | Wiki | Source | Report an Issue

Usage

CSRepl is implemented as a windows console application and to use it when installed, you simply start it with the csrepl command from the command line:

C:\Program Files (x86)\CSRepl\>csrepl
 
CSRepl 1.0 Interactive build 1.0.4383.22797
Copyright (c) trycsharp.org. All Rights Reserved.
 
For help type Help
 
> 

Features

Support includes, but does not stop at:

Literal expressions:

> 1 + 2
3
> 2 * 4
8
> "hello, world"
"hello, world"

Semicolon is optional:

> 1 + 2
3
> 1 + 2;
3

Sequence of statements:

> var x = 1
> x
1
> x = 2
2

Collections:

> new[] {1,2,3}
[1,2,3]
> var list = new List<int> {1,2,3}
> list
[1,2,3]
> var map = new Dictionary<int, string> {{1,"foo"},{2,"bar"}}
> map
[[1, "foo"],[2, "bar"]]

Method declarations:

> int Foo() { return 1; }
> Foo()
1

Type declarations:

> class Foo { public string Bar() { return "Bar"; } }
> var foo = new Foo()
> foo.Bar()
"Bar"

Multi line statements:

> class Foo
> {
>    public int AddOne(int v)
>    {
>        return v + 1;
>    }
> }
> var foo = new Foo()
> foo.AddOne(1)
2

Using statements:

> new ArrayList {1}
The type or namespace name 'ArrayList' could not be found
> using System.Collections
> new ArrayList {1}
[1]

Formatting of complex types:

> class Foo { public string Bar { get; set; } }
> var foo = new Foo { Bar = "Hello" }
> foo
Foo { Bar = "Hello" }
 
> var anonymous = new { X = 1, Y = "foo" }
> anonymous
{ X = 1, Y = foo }
 
> var xml = new XmlDocument();
> xml.LoadXml("<foo bar=\"baz\" />")
> xml
<foo bar="baz" />

Load external assemlies:

> LoadAssembly("ICSharpCode.SharpZipLib.dll")
> using ICSharpCode.SharpZipLib.Zip
> new FastZip()
FastZip { CreateEmptyDirectories = False,  Password = null,  
NameTransform = ZipNameTransform { TrimPrefix = null },  
EntryFactory = ZipEntryFactory { 
NameTransform = ZipNameTransform { TrimPrefix = null },  
Setting = LastWriteTime,  FixedDateTime = 2012-01-01 12:36:13,  
GetAttributes = -1,  SetAttributes = 0,  IsUnicodeText = False },  
UseZip64 = Dynamic,  RestoreDateTimeOnExtract = False,  
RestoreAttributesOnExtract = False }

Comments (9) -

  • Mikael Mogren

    2014-02-08 19:01:29 | Reply

    Impressive. Linqpad har f├ątt konkurrens

  • Mr. S

    2014-02-08 19:02:11 | Reply

    Nice! What license have you released it under?

    • Rickard

      2014-02-08 19:02:43 | Reply

      Thanks, glad you like it. Its released under the New BSD License. If you are willing to participate you can submit pull requests at http://bitbucket.org/rickardn/csrepl

  • Paul Shmakov

    2014-02-08 19:03:49 | Reply

    Thanks, Rickard! Useful tool, replaced my always open Powershell session.

  • Fred

    2014-02-08 19:05:43 | Reply

    New version soon? I hope!
    Also, you considered a GUI version?
    Also does it compile with Mono?
    Does it work on Linux?

    • Rickard

      2014-02-08 19:06:10 | Reply

      What are the features you're missing the most?
      I'm not considering a GUI version at this time. Have you looked at LinqPad? at http://www.linqpad.net/
      I have not tested it with Mono/Linux, however the Mono tooling already include a C# Repl at http://www.mono-project.com/CsharpRepl also available as a windows version but it runs on Mono (duh) and not the .NET framework.

  • Luke

    2014-02-08 19:08:07 | Reply

    Nice article. We've just released a C# REPL as well but with a UI, it even has code completion in the REPL, supports script files and references, but if you only want a REPL it doesnt get in the way.
    Check it out: http://cshell.net/

    We make heavy use of the mono C# evaluator since Roslyn is not yet ready.

  • IBIT.ZEE

    2014-02-27 03:36:02 | Reply

    Very good... it's the authentic REPL spirit...
    I've already try other C#Scripting and yours are the one I choose for my immediate tests...

    Why ?
    1)no GUI to load and fiddle... just load and go...
    2)Very much like NodeJS (nodejs.org)...
    3)The exe is soooooooo small (please put a zip for download, no installer)

    What I miss...
    1) Autocomplete/Intellisense (much like in nodejs... type 'tab' key and the available syntax/methods/properties are shown... filtered by the current typed text)
    2) Syntax coloring

    *Please download and run nodejs REPL to see it in action... I think its one of the best modern REPL env I ever saw... -> Simple and Powerfull... We need such a thing for using the .net library...

    Thanks

Loading
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.