Wednesday, March 21, 2018

Can I run C# code interactively in Visual Studio Community 2017?

Yes, of course.
Let us run a few calculations with numbers x and y and write the result.

We need to bring up the C# interactive screen. You can do this from the Start Page's View menu item as shown.


There are 4 toolbar items on C# Interactive, Left to right they are reset, clear, history up(up arrow) and history down (down arrow). The Compiler is rather slow.


You can enter line by line into this window as shown.
--------
> 2*3
6

-----------
After entering the line press ENTER.

If you want to enter multiple lines without executing each line, you can enter SHIFT+ENTER and that puts a new line starting from a dot, and you can enter the next line.

At the end of your statement you can enter to execute the code.

Here are a few lines of code execution interactively:

> int x = 4;
. int y = 6;
. x*y
24
> WriteLine(x*y)
24

You can also cut and paste code and run it. Notice that the new lines start with a dot.

This is the code.
void Multiply(int x, int y)
{
 WriteLine($"Multiplying {x} and {y} you get :{x*y}");
}




No comments: