在 C# 中为字符串分配双引号的最易读的方法
还有其他人认为在非常短的字符串中转义字符会使它们不太可读吗?我注意到我在代码中使用 s = "\""
为字符串分配双引号,但经过思考,我想出了以下替代方案: s = '" '.ToString()
。
- 我的替代方案好吗?您希望看到代码中的第一个版本吗?
- 如何将两个双引号 ("")(可能是
s = "\"\""
)分配给字符串?
/我在被迫进入该 CW 之前正在对其进行标记。
Does anyone else think that escaping characters in very short strings make them not very readable? I noticed I was using s = "\""
in my code to assign a double quote a string, but having thought about it, I came up with the following alternative: s = '"'.ToString()
.
- Is my alternative any good? Would you prefer see the first version in code?
- How would you go about assigning two double quotes (""), which might be
s = "\"\""
, to a string?
/me is marking this CW before being pressured into it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以使用:
或者如果你喜欢迷惑人们:
但实际上我仍然更喜欢老式的转义:\"
You could use:
or if you like to confuse people:
but actually I still prefer the good-old-fashioned escape: \"
我不确定替代方案是否更具可读性,相反它令人困惑。此外,使用函数调用在源代码中获得不同的外观没有多大意义 - 我什至会说这是不好的做法。
恕我直言,老式的转义序列是最好的选择。
I'm not sure the alternative is more readable, on the contrary it's confusing. Besides, using a function call to have a different look in the source code doesn't make much sense - I would even say it's bad practice.
The old-fashioned escape sequence is the best option IMHO.