Sunny Ahuwanya's Blog

Mostly notes on .NET and C#

Introducing C# Pad

I’m excited to present C# Pad, an interactive web based C# REPL.

Have you ever wanted to quickly evaluate an expression or test some code, like say try out different DateTime string formats or test a method or clear up some confusion (like what’s the difference between Uri.EscapeDataString and Uri.EscapeUriString or what new Random().Next(0) returns), or decode some string in Base64 or some other format?

C# Pad lets you easily do all those things and a whole lot more.

Interactive REPL

Do you see the embedded code pad below? Go ahead and hit the Go button.

var greeting = "こんにちは世界";
Console.WriteLine("{0} in Japanese is {1}", "Hello World", greeting);

After the submission is processed, you’ll see the result of the code evaluation.
Now, in the same code pad, type Console.WriteLine(greeting.Length) and hit Go.

As you can see, the greeting variable from the previous submission is accessible in the code editor.
That’s because C# Pad is a REPL. Objects in previous submissions are visible and accessible from the current one.
( Did you also notice the cool code completion? :) )

You don’t need to call Console.WriteLine to display results. Simply type the variable name (without a semicolon) in the last line of a code submission and the string representation of the variable’s value will be displayed. 
For example, type greeting in the same code pad and hit Go to see its value.

The following code pad contains a method that encodes an input string in Base64 format.

using System.IO;
using System.Text;

string Base64Encode(string input)
    if(input == null) throw new ArgumentNullException("input");
    var bytes = Encoding.UTF8.GetBytes(input);
    return Convert.ToBase64String(bytes);

Hit Go to submit the method and then type Base64Encode("My string") (do not include a semicolon) and hit Go.

You’ll observe the result is a string in Base64 format, encoded by the method that was defined in an earlier submission. Because the semicolon was left out, the result of the evaluation was displayed.

You can use C# Pad to write complex code, define classes and methods and evaluate all kinds of expressions ranging from simple mathematical expressions like 60 * 60 * 24 or Math.Sin((30 * Math.PI)/ 180) to LINQ expressions.

(from c in "The quick brown fox jumps over the lazy dog".ToLower()
group c by c into grp
select new { Letter = grp.Key, Count = grp.Count() }).ToArray()

C# Everywhere

You can embed interactive code pads in your blog, website or any site you can edit, just like I have in this blog post. You can even select a theme that matches your site.
Yes, this means you can now create a C# playground anywhere. Simply visit, compose the code you’d like to embed, click the embed button and follow the instructions.  

Here’s an example JSBin with an embedded code pad.

You can also load and save Github Gists.
To load a Gist, open or simply

As examples, the links below open up Gists in C# Pad. ( Did you know The Octocat codes in C#? )

To open a single file in a Gist, append ?file_name at the end of the url, like so

Numerical Analysis

The awesome Math.Net Numerics library is bundled with C# Pad, which makes working on complex math and statistics problems a breeze.

The following program integrates the function f(x) = exp(-x/5) (2 + sin(2 * x)) on a closed interval of [0, 100].

using MathNet.Numerics;
Integrate.OnClosedInterval(x => Math.Exp(-x / 5) * (2 + Math.Sin(2 * x)), 0, 100)

The following program generates ten samples of a Poisson distribution with a lambda parameter of 1.

using MathNet.Numerics.Distributions;
var poisson = new Poisson(1);
for(var i = 0; i < 10; i++)
    Console.WriteLine("{0:N05}", poisson.Sample());

The following program calculates the 75th percentile of an array of numbers.

using MathNet.Numerics;

var percentile = 75;
var array = new double[]{ 89.6, 33.5, 11.6, 44.3, 66.78, 34.78, 97.1, 68.0, 25.7, 48.7};

ExcelFunctions.Percentile(array, percentile / 100d)

The following program multiplies the transpose of a matrix with another matrix.

using MathNet.Numerics.LinearAlgebra.Double;

var matrixA = DenseMatrix.OfArray(new[,] { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 } 
var matrixB = DenseMatrix.OfArray(new[,] { { 1.0, 3.0, 5.0 }, { 2.0, 4.0, 6.0 }, { 3.0, 5.0, 7.0 } });

matrixA.Transpose() * matrixB

//Same as matrixA.TransposeThisAndMultiply(matrixB)


C# Pad is the first and only (as of this writing) web based interactive C# REPL, with code completion, diagnostics, themes, embeddability, timing information, Gist support and more features to come.

I hope you find C# Pad useful and delightful. Drop me a line or use the help link on the site to provide feedback on feature suggestions, bug reports and other kinds of feedback.

Have fun!

Comments (13) -

  • jbriguet

    2/26/2014 2:34:07 PM | Reply

    Pretty cool !

    I keep having a lot of Server Error: timeout, but otherwise, it's quite awesome !

    • Sunny

      2/27/2014 8:42:49 PM | Reply

      Thanks, there was a lot of traffic on the site when it was released which led to the server errors. It's much better now.

  • YJ

    2/27/2014 11:47:10 PM | Reply

    truly awesome! What I exactly wanted 2 hours ago (light scratchpad for C#ers)

  • Naveen Bhat

    2/28/2014 4:49:54 AM | Reply

    Nice concept Smile

    I would like to use it in my blog for future articles. Currently, its bit slow but if the performance is increased then its completely awesome.

    • Sunny

      3/2/2014 7:01:53 PM | Reply

      I've fixed a bug that caused the performance problems. It's a lot better now.
      Feel free to embed it in your blog.

  • Arad

    3/8/2014 12:46:41 AM | Reply

    good job, thanks

  • Arya

    9/26/2014 4:34:45 AM | Reply

    Great job...truely helpful..Smile

  • Chris Cavanagh

    9/30/2014 11:52:52 AM | Reply

    Sunny - This must be using Roslyn, right?

    • Sunny

      9/30/2014 7:23:59 PM | Reply

      Yes Chris, it uses Roslyn.

  • Amit Joki

    10/8/2014 8:08:50 AM | Reply

    Can you make a downloadable version (free would be great!!!) so that I can use it offline?

  • Mike Henderson

    10/9/2014 9:58:21 AM | Reply

    This is really cool. Thanks for sharing.

  • Abhijeet Nagre

    12/23/2014 5:10:19 AM | Reply

    Awesome work.

  • Julien

    1/1/2015 10:35:16 PM | Reply

    Hey Sunny,
    Just wanted to say thanks!  This is a pretty awesome tool.

    I'd love to give you a donation for your work, if you accept them.

    If you're taking requests, my wishlist would include allowing Console.ReadLine() as a way of interacting with the code snippets we're creating.  Currently it does execute, but simply reads in a null string without actually waiting for input from the user.  No idea how much work this would be for you (and security-wise, I don't know if you'd need to do more than fully escape the input), but figured I'd suggest it.

    Thanks again!