有没有办法在 c# 中注释掉字符串的一部分?

Dud*_*arn 1 c# string comments

我在这里得到了这个代码部分:

label1.Text = $"Score: {score} | Speed: {speed}";
Run Code Online (Sandbox Code Playgroud)

这显示了我制作的突破游戏的得分和速度。现在我不需要速度,我想知道是否有办法注释掉字符串的一部分。

我当然可以

label1.Text = $"Score: {score}";// | Speed: {speed};
Run Code Online (Sandbox Code Playgroud)

但也许还有另一种方法,因此可以更轻松地删除评论。就像是

label1.Text = $"Score: {score} #comment | Speed: {speed} #endcomment";
Run Code Online (Sandbox Code Playgroud)

或者

label1.Text = $"Score: {score} #/*| Speed: {speed} #*/";
Run Code Online (Sandbox Code Playgroud)

所以更容易阅读和更改

Fil*_*dor 6

您可以使用预处理器指令,而不是注释掉:

#if DEBUG
    label1.Text = $"Score: {score} | Speed: {speed}";
#else
    label1.Text = $"Score: {score}";
#endif
Run Code Online (Sandbox Code Playgroud)

DEBUG 应在调试模式下定义。这是 Visual Studio 中的默认设置。所以你不需要总是注释和注释并记住不要让它进入发布输出。

请注意,您不应该过度使用它。从长远来看,其中许多会使您的代码变得混乱并使其不可读(和维护地狱)。不过,对于像这里这样的特定小用途,应该没问题。