.NET Zone
.NET Zone is brought to you in partnership with:
spacer spacer Filip Ekberg
  • Bio
  • Website
  • @fekberg

I am a Software Engineer working with the latest in .NET focusing on Web Development. With a burning heart for programming, I blog and author a book about C#. Filip is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Using the C# Interactive Window that comes with Roslyn

07.18.2012
spacer Email
Views: 1505
  • Tweet
  • spacer
This article is part of the DZone .NET Zone, which is brought to you in collaboration with the .NET community. Visit the .NET Zone for additional tutorials, videos, opinions, and other resources on this topic.

We Recommend These Resources

VaraLogix Q Product Demo - Application Deployment Automation & Configuration Management in One Solution

Micro Focus Silk Performer

Getting Started with Apache Camel - 6/28/2012 - 10:00AM EST

NOSQL for the Enterprise

DevOps: From Concepts to Practical Applications Featuring Forrester Research, Inc. and Equinox

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:

spacer

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

let x = 10;;

And then it will print:

val x : int = 10


Which means it will look somewhat like this in your F# Interactive Window:

spacer

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:

View -> Other Windows -> C# Interactive Window

spacer

The C# Interactive Window looks very much like the F# one:

spacer

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!

spacer

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:

#cls


Now let’s see if we can download the contents for this blog using a WebClient, if you start off by just writing

var client = new WebClient()

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!

spacer

Once it is imported, it will look like this:

spacer

Now let’s download some data! Add the following on the next line:

var data = client.DownloadString("blog.filipekberg.se");

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!

spacer

Here’s a final example of what you can do:

spacer

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!

Published at DZone with permission of Filip Ekberg, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:
  • roslyn
  • Tutorial
  • C-Sharp
  • .NET & Windows
This content was brought to you by DZone for all the information you need on ASP.NET, WPF, XAML, SQL Server, and other pieces of the .NET stack.
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.