SharpLab - Another Powerful Online Code Execution And Decompiler Tool

There are a handful of tools (online/offline) that let you write C# (and even VB, F#) code and execute it without the need of creating a project. A few of them are listed below:

  •   LINQPad  -  Offline tool
  •  .NET Fiddle - Online tool  - https://dotnetfiddle.net/
  •  JDoodle - Online tool - https://www.jdoodle.com/
  •  Try Dot Net ( Microsoft's recommendation ) -  Online tool -  https://try.dot.net/

Adding to this list is 'SharpLab' ( Online tool ).

Although the original intention of creating the above products was to provide developers a playground where they can quickly run their code and see the output, these products evolved over time and now provide a lot of great features.

It takes nearly a dozen articles to cover the features of all those tools. Even though I would love to do it, I prefer not to do it because there are lot of articles already (in C# Corner) that cover these tools - Please do a quick search when you have time.

Now back to our topic ... SharpLab is a online playground that lets you : 

  1. Write C# / F#/ VB code snippets and Verify / Execute them.
  2. Inspect the MSIL code
  3. Decompile the MSIL code
  4. View Syntax Tree
  5. View Native ASM Code

This article covers (1), (2) and (3) and will try to cover (4) and (5) in the future article as the latter are kind of advanced topics.

1) Write C# / F#/ VB code snippets and Verify/Execute them

Go to https://sharplab.io/, Write your code and execute it.

2) Inspect the MSIL code

Every .NET developer knows what MSIL ( Microsoft Intermediate Language ) is. So, I am not going to explain it. But, while I show you some MSIL code with SharpLab, I will try to cover a few important things that you already know but MIGHT NOT have seen before.

System.Object is the base class of every type in .NET:  The below MSIL code ( right side ) clearly shows this statement is true. 

int is the same as int32:   Let's look at MSIL again that proves it's correct -  int (left side) is a C# synonym of int32 (right side) in BCL ( base class library type )

Properties and Methods are same behind the scenes:  Properties and Methods let us write code in a pure Object oriented manner. However, under the hood both are the same. Infact Properties get converted to Methods internally

Comments make code readable. They don't have any role in code execution. And so, they are removed from MSIL code.   As you can see in the below code. the comment I wrote ( left side ) is missing in MSIL code ( right side )

There are lot more things that you can observe which proves a lot of things ( that were just theory once ). I  leave it to you guys to explore further.

3) Decompile the MSIL code:  This is reverse engineering - Conver MSIL code back to C#.  Please note that the code won't be exactly same as the original. For example, see how the properties are generated below - It still works, however the names aren't so friendly.

So, this is it, guys. Before we end this topic, I would like to tell you that I am in NO way promoting or saying SharpLab is better than other tools. I am just sharing what I have found. In fact, every tool I mentioned at the beginning of the article is unique and comes with great features. And of course, nowadays our code editors itself include many of these features. But, that's a topic for another day.

Thanks for reading. Your comments are always welcome :)