DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Using the C# Interactive Window that comes with Roslyn

Using the C# Interactive Window that comes with Roslyn

Filip Ekberg user avatar by
Filip Ekberg
·
Jul. 18, 12 · Interview
Like (0)
Save
Tweet
Share
5.50K Views

Join the DZone community and get the full member experience.

Join For Free

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

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:

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

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:

#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!

Once it is imported, it will look like this:

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

var data = client.DownloadString("http://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!

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!

Testing code style Data Types Data (computing) Command (computing) Download Concept (generic programming) Moment

Published at DZone with permission of Filip Ekberg, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • Core Machine Learning Metrics
  • Why You Should Automate Code Reviews
  • Why Every Fintech Company Needs DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: