Tech

Ctrl + E, E – the best Visual Studio shortcut I’ve seen so far!

I can’t be the only person who hates waiting for the code to compile and run… Especially when I want to test only a small part of it! That’s a total waste of time! But good news everyone, I found a way to run a part of my C# code without building the whole project!

Ctrl + E, E – the magical shortcut!

Ok, so if we want to run just the fragment of our code – this is what we do:
1.    Select the fragment of code we want to run. This has to be a fully compilable fragment so make sure that if you copy the code to the separate project, it would compile. Depending on the Visual Studio version, we need to type only class names or the class names with namespaces.
2.    Press Ctrl + E, E and wait for the magic to happen!

We will see the ‘C# interactive’ pane activate in the bottom of the Visual Studio window.

What is going on here? Well, the Roslyn (known as a ‘Compiler as A Service’) takes your C# code and runs it! Amazing, isn’t it?

Show me something usefull!

Ooook, why don’t we create a text file? Let’s write the following C# code:

using System.Collections.Generic;
using System.IO;
using System.Web.Mvc;

namespace RunTester.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            List<string> s = new List<string>() { "a" };

            FileInfo file = new FileInfo(@"c:\bambam.txt");
            file.Create();

            return View();
        }
    }
}

And let’s test the lines that create the file so we select the below lines and press Ctrl + E, E.

FileInfo file = new FileInfo(@"c:\bambam.txt");
file.Create();

Now we check the c: drive. Yep, the file bambam.txt is there!

 

Ok, but what if I want to run the code that requires some assembly?

Not a problem, we just use #r directive! For example, for our custom assembly, we just type the following code in the C# Interactive tab :

#r "path/MyVeryImportandLib.dll"

That’s even easier when we want to use a .net library:

#r "System.Collections.Generic"

Example needed? Here we go! Let’s assume I have a class library  ‘Helpers‘ with a class ‘Cast‘ and a method ‘StringToInt()’ . And we want to run this method in another project, without compiling anything. Without the ‘#r…’ directive we will receive the following error:

To get rid of this error, we have to:

  1. In ‘C# Interactive‘ pane type #r directive with the absolute path to the library. And press enter, of course :).
  2. Select the code we want to run and press Ctrl + E + E.

We will see the result now! Wow, it works! 😉

 

We also can reference the used assembly just in the middle of our code! Like in the following example:

using System.Collections.Generic;
List<int> myList = new List<int> { 3, 2, 7, 4, 9, 0 };
var sum = myList.Where(x => x % 2 == 0).Sum();

 

Yep, this time Roslyn also did everything for us and returned the number “6” (which is a sum of even numbers in the list myList) as a result in the Console.

Featured image by Andy Mai

Share this: