Vit*_*ova 18 c# read-eval-print-loop roslyn visual-studio-2015 c#-interactive
我开始使用C#Interactive,就像我可以浏览和探索一些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
>
Run Code Online (Sandbox Code Playgroud)
有没有办法C# Interactive
在每个评估中输出值,就像Immediate
那样(并且没有为此编写更多代码Console.Write
)?
Cro*_*der 27
是的,要输出已计算表达式的结果,请不要在末尾添加分号.在您的示例中,而不是:
string.Format("{0,15}", 10m);
Run Code Online (Sandbox Code Playgroud)
做这个:
string.Format("{0,15}", 10m)
Run Code Online (Sandbox Code Playgroud)
当你完成一个语句(例如以...结尾;
)时,你必须在声明变量时,你没有得到任何输出,因为它应该只有副作用.
当您使用表达式(例如,不以...结尾;
)时,您将获得该表达式的结果.解决方法是:
var a = string.Format("{0,15}", 10m); a
Run Code Online (Sandbox Code Playgroud)
请注意a
最后的表达式,您将打印其值.
就个人而言,对于我想测试的多行片段,我通常有一个res
变量:
object res;
// code where I set res = something;
using (var reader = new System.IO.StringReader("test"))
{
res = reader.ReadToEnd();
}
res
Run Code Online (Sandbox Code Playgroud)
每个Visual Studio会话都会发生一次打字开销,但之后我只使用Alt+ ↑来选择之前的一个条目.
归档时间: |
|
查看次数: |
2981 次 |
最近记录: |