Using the C# Interactive Window that comes with Roslyn
Posted by Filip Ekberg on November 14 2011 4 Comments
Posted by Filip Ekberg on November 14 2011 4 Comments
I often find myself wanting to explore new options and see what is possible to do and what is not, at other times I might really need to test something fast just to see if my concept will work. In the past I’ve either created a test for this, mocking the stuff that I need and then finding myself debugging the test. Or I just write a method and use my test-extension to invoke the method that I right click ( cool feature from TestDriven.NET ).
While both of these tend to work out very well and the first one might even be a good approach, since we all want 100% code coverage! But, what about the times when you just want to explore something or just test something out? If you’re an F# developer you might have seen the F# Interactive Window that looks like this:
With this we can explore F# and write code that is evaluated directly, this is called REPL which is short for Read-eval-print loop. This means that we can create statements like this
And then it will print:
Which means it will look somewhat like this in your F# Interactive Window:
I can tell you, this is very, very, very(!) useful at some times, I found myself wanting to test a regex in a couple of different ways and having an Interactive Window like this is very helpful. The above is for F# and is built into Visual Studio, but what about a C# Interactive Window?
Out of the box, there is none. But if you install Roslyn ( which is a CTP at the moment ) you’ll get one! You’ll find it just below the F# Interactive Window in:
The C# Interactive Window looks very much like the F# one:
You might have notice that I haven’t said that you need to create a new project, this is because you don’t have to, you can just fire up Visual Studio and start playing around in the C# Interactive Window! Now let’s see what this baby can do!
We can start off by checking the help, you do this by writing #help and pressing enter, notice while you write that you have intellisense here!
The help-text isn’t that long, read through it and understand the different key-shortcuts for executing segments of code. There is another built in command that you might find useful and that is “cls” which for all you none-DOS-aware people is short for “clear screen”. All the built in commands are executed by writing a leading “#” so to clear the screen, simply write the following and press enter:
Now let’s see if we can download the contents for this blog using a WebClient, if you start off by just writing
You’ll see that WebClient isn’t known where and we get a underscore below the W which indicates that we can press ctrl+dot and import the namespace!
Once it is imported, it will look like this:
Now let’s download some data! Add the following on the next line:
When you press enter, the statement will be executed and this might take a couple of seconds. Notice that nothing has been printed out or evaluated yet and by that I mean, in the F# Interactive Window after each statement you got some data printed out about what the last statement generated, that’s not the case in the C# Interactive Window, but! If you write the name of the variable we just created, you’ll see something pretty cool!
Here’s a final example of what you can do:
I think this is a very nice tool to use when you just want to try stuff out, I found myself wanting to try some different regexes so I fired up the C# Interactive Window and started testing and the easiness of just dumping out what stuff is, it’s pretty neat!
I hope you found this interesting, if you have any thoughts please leave a comment below!
?>









Filip Ekberg is a Senior Software Engineer working in the country with all the polar bears, author of a self-published C# programming book and overall in love with programming.





Pingback: The Morning Brew - Chris Alcock » The Morning Brew #982
Hi,
Thank you for the post.
Please, can you tell me how can I call “C# Interactive Window” in Visual Studio 2012 Ultimate? I installed Roslyn, but, I can’t find it in View>Other Windows.
I was able to call it in VS2010, but, I’ve upgraded my work to 2012.
Regards
Hi,
It should be available in the same place, but it is now just called “C# Interactive”, look at my screenshot below. Did you install the latest CTP?
Hi,
Thank you very much for reply.
Actually, I did install the latest version of Roslyn CTP, which asked me to install Visual Studio SDK, I installed it too.
I was afraid that the VS 2012 doesn’t support it, but now, I know that it supports it. Which is all what I need to make my goal. I’ll try it later.
Thank you again.
Best regards, Osama.