与 C# 中的 string+=int 行为不一致

发布于 2024-09-16 21:20:18 字数 218 浏览 10 评论 0原文

我正在查看 LINQPad 中的一些代码高尔夫,想知道为什么:

int c;
string o;

o+=c;//this works

o+=P==2?"."+c:c;//this doesn't

o+=P==2?"."+c:""+c;//this does

主要是为什么第一个有效,而第二个抛出“‘string’和‘int’之间没有隐式转换”错误。

I'm looking at some code golf in LINQPad and wondering why:

int c;
string o;

o+=c;//this works

o+=P==2?"."+c:c;//this doesn't

o+=P==2?"."+c:""+c;//this does

mostly why the first one works and the second one throws a "no implicit conversion between 'string' and 'int'" error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你曾走过我的故事 2024-09-23 21:20:18

字符串上的 + 运算符可以接受 int,从而产生另一个字符串。但是,不存在从 int 到 string 的隐式(或显式)转换。

当您使用三元运算符 ?: 时,两个“分支”必须是相同的类型,或者其中一个的类型必须可以隐式转换为另一个。

在第二个示例中,在 + 运算符完成后,第一个分支是一个字符串,但第二个分支只是一个 int,因此它不起作用。在你的第三个例子中,两个分支都是字符串,所以没问题。

The + operator on strings can accept an int, resulting in another string. However, there is no implicit (or explicit) cast from int to string.

When you use the ternary operator ?:, both "branches" need to either be the same type, or the type of one has to be implicitly convertible to the other.

In your second example, the first branch is a string, after the + operator is done, but the second is just an int, so it doesn't work. In your third example, both branches are strings, so it's fine.

旧城空念 2024-09-23 21:20:18

您的第二个非工作示例在 ?: 运算符中具有不一致的类型。你所拥有的是:(

o += (P == 2 ? (string) "." + c : (int) c);

上面括号中的类型是为了澄清现有类型是什么,而不是将它们转换为不同的类型。)

的两侧:三元运算符中的必须是同一类型。因此,您的第二个示例是语法错误。第三个示例有效,因为与空字符串连接会将 c 强制转换为字符串。

Your second, non-working example has inconsistent types in the ?: operator. What you have is:

o += (P == 2 ? (string) "." + c : (int) c);

(The types in parentheses above are there to clarify what the existing types are, not to cast them to a different type.)

Both sides of the : in the ternary operator must be of the same type. For this reason, your second example is a syntax error. The third example works, because concatenating with an empty string coerces c into a string.

空名 2024-09-23 21:20:18

+= 运算符使用 + 运算符,因此第一个实际上是:

o = o + c;

编译器实际创建的是:

o = String.Concat((object)o, (object)c);

整数被装箱,并且 Concat 调用采用object 参数的方法。将对两个参数调用 ToString 方法以获取它们的字符串值,并将它们连接起来。

如果您首先将整数转换为字符串,代码会变得更加直接:

o += c.ToString();

变成:

o = String.Concat(o, c.ToString());

在第二个代码中,条件运算符中的类型不匹配:

bool ? string : int

第二个和第三个操作数必须具有相同的类型,如第三段代码:

bool ? string : string

第二段代码实际上变为:

o = String.Concat(
  o,
  P == 2
    ? String.Concat((object)".", (object)c)
    : c
);

第三段代码实际上变为:

o = String.Concat(
  o,
  P == 2
    ? String.Concat((object)".", (object)c)
    : String.Concat((object)String.Empty, (object)c)
);

无论如何,您应该考虑使用 StringBuilder 来构建字符串,而不是使用 += 运算符:

StringBuilder builder = new StringBuilder;

if (P == 2) {
  builder.Append('.');
}
builder.Append(c);

The += operator uses the + operator, so the first is really:

o = o + c;

What the compiler actually creates is:

o = String.Concat((object)o, (object)c);

The integer is boxed, and the Concat method that takes object parameters is called. The ToString method will be called on both parameters to get their string values, and they are concatenated.

If you first convert the integer to a string, the code gets more straight forward:

o += c.ToString();

which becomes:

o = String.Concat(o, c.ToString());

In the second code, the types in the conditional operator doesn't match:

bool ? string : int

The second and third operand has to have the same type, like in the third code:

bool ? string : string

The second code really becomes:

o = String.Concat(
  o,
  P == 2
    ? String.Concat((object)".", (object)c)
    : c
);

and the third code really becomes:

o = String.Concat(
  o,
  P == 2
    ? String.Concat((object)".", (object)c)
    : String.Concat((object)String.Empty, (object)c)
);

Anyhow, you should consider using a StringBuilder to build strings instead of using the += operator:

StringBuilder builder = new StringBuilder;

if (P == 2) {
  builder.Append('.');
}
builder.Append(c);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文