什么是$ - 运算符应该意味着一个字符串? [英] What's $-operator supposed to mean for a string?
本文介绍了什么是$ - 运算符应该意味着一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我刚刚说了下面在评论部分。
So, I've just had the following conversation with a user in the comments section.
我:
Year year = new Year{ State = States.Happy };
它们:
Them:
eventStream.ReceiveJoke += joke =>
Console.WriteLine($"Pretty nice joke: {joke}, Thanks!!!");
和,书呆子,因为我,我不知道他是什么意思美元符号,但我认为它太尴尬地问他。
And, nerdy as I am, I wonder what he meant by the dollar sign but I think it's too embarrassing to ask him.
推荐答案
这是一个插字符串,在C 6#出台。
It's an interpolated string literal, introduced in C# 6.
这是大致等同于:
eventStream.ReceiveJoke += joke =>
Console.WriteLine(string.Format("Pretty nice joke: {0}, Thanks!!!", joke));
编译器查找括号与 $ $引入的任何字符串内C $ C>,并适用字符串格式化它。您可以使用(大部分)任意表达式,而不仅仅是变量,如:
The compiler looks for braces within any string literal introduced with $
, and applies string formatting to it. You can use (mostly) arbitrary expressions, rather than just variables, e.g.
Console.WriteLine($"{year.State} {2000 + 16}"); // Happy 2016
这篇关于什么是$ - 运算符应该意味着一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文