Is it possible to automatically output a value in C # Interactive (REPL), e.g. Immediate?

I started using C # Interactive and, as a fact, I can view and learn some of the functionality of the API, as I do with Immediate without having to start and debug my program.

The problem is that it does not display information of type Immediate, unless I execute a command with a variable name:

  > string.Format("{0,15}", 10m); //hit enter, here there is no output > var a = string.Format("{0,15}", 10m); //hit enter so... > a // hit enter and... " 10" //...here the value is shown > 

Is there a way to make C# Interactive output the values ​​in each evaluation, for example, Immediate (and not write more code for this type of Console.Write )?

+7
c # read-eval-print-loop visual-studio-2015 roslyn c # -interactive
source share
2 answers

Yes, to display the result of the evaluated expression, just do not put a semicolon at the end. In your example, instead:

 string.Format("{0,15}", 10m); 

do the following:

 string.Format("{0,15}", 10m) 

See documentation

+12
source share

When you are done with the expression (for example, ending with ; ), which you should when declaring variables, you will not get any output, since it should have only side effects.

When you are done with the expression (for example, not ending ; ), you will get the result of this expression. Workaround:

 var a = string.Format("{0,15}", 10m); a 

Notice a as an expression at the end, you get its value.


Personally for multi-line snippets that I want to check, I usually have a res variable:

 object res; // code where I set res = something; using (var reader = new System.IO.StringReader("test")) { res = reader.ReadToEnd(); } res 

Input overhead occurs once for a Visual Studio session, but then I just use Alt + ↑ to select one of the previous entries.

+6
source share

All Articles