Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有可能像即时操作一样,在C#Interactive(REPL)中自动输出值?_C#_Visual Studio 2015_Roslyn_Read Eval Print Loop_C# Interactive - Fatal编程技术网

有没有可能像即时操作一样,在C#Interactive(REPL)中自动输出值?

有没有可能像即时操作一样,在C#Interactive(REPL)中自动输出值?,c#,visual-studio-2015,roslyn,read-eval-print-loop,c#-interactive,C#,Visual Studio 2015,Roslyn,Read Eval Print Loop,C# Interactive,我开始使用并喜欢这样一个事实:我可以像使用Immediate一样浏览和探索一些API功能,而无需运行和调试我的程序 问题在于,除非我使用变量名执行命令,否则它不会像Immediate那样输出信息: > string.Format("{0,15}", 10m); //hit enter, here there is no output > var a = string.Format("{0,15}", 10m); //hit enter so... > a

我开始使用并喜欢这样一个事实:我可以像使用
Immediate
一样浏览和探索一些API功能,而无需运行和调试我的程序

问题在于,除非我使用变量名执行命令,否则它不会像Immediate那样输出信息:

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

有没有一种方法可以让
C#Interactive
Immediate
那样输出每次求值中的值(而不需要为类似
控制台.write
)这样的操作编写更多的代码?

是的,要输出求值表达式的结果,不要在末尾加分号。在您的示例中,不是这样:

string.Format("{0,15}", 10m);
这样做:

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

当您以声明变量时必须使用的语句(例如,以
结尾)结束时,您不会得到任何输出,因为它被认为只会产生副作用

当您使用表达式结束时(例如,不以
结束;
),您将得到该表达式的结果。解决办法是:

var a = string.Format("{0,15}", 10m); a
注意,
a
作为结尾的表达式,您将得到它的值


就个人而言,对于我想要测试的多行代码片段,我通常有一个
res
变量:

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

键入开销在每个VisualStudio会话中发生一次,但我只使用Alt+↑ 选择前面的一个条目。

Nice。谢谢但是@Crowcoder给出的答案更像是我在寻找的。投了赞成票。好吧,我现在知道你的问题比刚才说的要简单。我的回答是,您打算将值存储在一个变量中并打印它,而不是仅仅打印它。很抱歉造成混淆。哇。我怎么会错过呢。谢谢