附加到字符串
URL 是一个字符串,那么为什么我不能像这样连接:
string url = "something";
url + string.Format("group={0}&", Id);
这是因为字符串是引用类型,并且它实际上试图将其添加到引用而不是对象中吗?
实现我想要的最好方法是什么?
URL is a string, so why can't I concatenate like so:
string url = "something";
url + string.Format("group={0}&", Id);
Is this because string is a reference type, and it's actually trying to add it to the reference rather than the object?
What is the best way to achieve what I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
需要分配
或
Needs assignment
or
+
运算符不会更改您在表达式中使用的变量。对于数值也不会这样做:您必须将结果分配给字符串变量:
您还可以使用
+=
运算符,这将产生完全相同的运行时代码:请注意这不会更改字符串,而是从两个字符串生成一个新字符串。代码实际做的事情是:
The
+
operator doesn't change the variable that you use in the expression. It doesn't do that for a numeric value either:You have to assign the result to a string variable:
You can also use the
+=
operator, that will produce the exact same runtime code:Note that this doesn't change the string, it produces a new string from the two strings. What the code actually does is:
您可以使用
连接到
mynewstring
或连接到
url
。原因是字符串是不可变的。要引用 MSDN:
You could use
To concatenate into
mynewstring
or alternativelyTo concatenate into
url
.The reason is that strings are immutable. To quote MSDN:
System.String
的实例是不可变的:对它们的任何变异操作(包括串联)都会生成一个带有结果的新实例。如果您不使用此值,您将不会看到预期的效果:或者当然
建议:您可能想要使用
UriBuilder
而不是进行字符串连接来构建 URL。Instances of
System.String
are immutable: any mutating operation on them (which includes concatenation) will produce a new instance with the result. If you don't use this value, you won't see the intended effect:or of course
Suggestion: You might want to use
UriBuilder
instead of doing string concatenations to build your url.有几个答案提到了不变性,但事实并非如此。
该代码行
作为独立语句根本不合法。你会得到错误
使 System.String 可变不会改变这一点。使
+
改变一个或两个操作数不会改变这一点。该行代码根本不合法。需要明确的是,您可以定义自己的类型,定义一个
+
运算符,在该运算符的方法内改变操作数之一(这可能会激怒您的消费者,无论是否可变类型),然后返回它,仍然不会使独立行a + b;
合法。您必须在涉及上述表达式之一(赋值、调用等)的某物中使用其结果。Several answers mention immutability, and that's not what this is.
The line of code
Is simply not legal as a standalone statement. You will get the error
Making
System.String
mutable would not change this. Making+
mutate one or both of the operands would not change this. The line of code simply is not legal.To be clear, you can define your own type, define a
+
operator, inside the method of that operator mutate one of the operands (which would probably anger your consumers, mutable type or not), and then return it, and it still would not make the standalone linea + b;
legal. You must use its result in something that is involved in one of the above expressions (assignment, call, etc.).字符串是不可变的,这意味着字符串一旦创建就无法修改。但是,您可以创建一个新字符串,这正是
+
运算符应用于字符串时所做的操作。由于它创建一个新字符串并且不会修改现有字符串,因此您需要像其他答案提到的那样分配它。这适用于所有不可变类型,例如整数,例如
Strings are immutable, meaning once one has been created it cannot be modified. You can however create a new string, which is exactly what the
+
operator does when applied to strings. Since it creates a new string and does not modify the existing one you will need to assign it like the other answers mention.This applies to all immutable types, such as integers, e.g.