附加到字符串

发布于 2024-12-08 23:31:57 字数 195 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(6

£烟消云散 2024-12-15 23:31:57

需要分配

url += string.Format("group={0}&", Id);

url = url + string.Format("group={0}&", Id);

Needs assignment

url += string.Format("group={0}&", Id);

or

url = url + string.Format("group={0}&", Id);
彼岸花似海 2024-12-15 23:31:57

+ 运算符不会更改您在表达式中使用的变量。对于数值也不会这样做:

int i = 42;
i + 4; // doesn't change the variable i

您必须将结果分配给字符串变量:

url = url + string.Format("group={0}&", Id);

您还可以使用 += 运算符,这将产生完全相同的运行时代码:

url += string.Format("group={0}&", Id);

请注意这不会更改字符串,而是从两个字符串生成一个新字符串。代码实际做的事情是:

string temp = String.Concat(url, string.Format("group={0}&", Id));
url = temp;

The + operator doesn't change the variable that you use in the expression. It doesn't do that for a numeric value either:

int i = 42;
i + 4; // doesn't change the variable i

You have to assign the result to a string variable:

url = url + string.Format("group={0}&", Id);

You can also use the += operator, that will produce the exact same runtime code:

url += string.Format("group={0}&", Id);

Note that this doesn't change the string, it produces a new string from the two strings. What the code actually does is:

string temp = String.Concat(url, string.Format("group={0}&", Id));
url = temp;
温柔嚣张 2024-12-15 23:31:57

您可以使用

var mynewstring = url + string.Format("group={0}&", Id);

连接到 mynewstring

url += string.Format("group={0}&", Id);

连接到 url

原因是字符串是不可变的。要引用 MSDN

字符串是不可变的——字符串对象的内容不能改变
尽管语法使得它在创建对象后发生了变化
看起来好像你能做到这一点。

You could use

var mynewstring = url + string.Format("group={0}&", Id);

To concatenate into mynewstringor alternatively

url += string.Format("group={0}&", Id);

To concatenate into url.

The reason is that strings are immutable. To quote MSDN:

Strings are immutable--the contents of a string object cannot be
changed after the object is created, although the syntax makes it
appear as if you can do this.

那些过往 2024-12-15 23:31:57

System.String 的实例是不可变的:对它们的任何变异操作(包括串联)都会生成一个带有结果的实例。如果您不使用此值,您将不会看到预期的效果:

string url = "something";
url = url + string.Format("group={0}&", Id);

或者当然

url += string.Format("group={0}&", Id);

建议:您可能想要使用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:

string url = "something";
url = url + string.Format("group={0}&", Id);

or of course

url += string.Format("group={0}&", Id);

Suggestion: You might want to use UriBuilder instead of doing string concatenations to build your url.

望笑 2024-12-15 23:31:57

有几个答案提到了不变性,但事实并非如此。

该代码行

a + b; 

作为独立语句根本不合法。你会得到错误

只有赋值、调用、递增、递减和新对象表达式可以用作语句。

使 System.String 可变不会改变这一点。使 + 改变一个或两个操作数不会改变这一点。该行代码根本不合法。

a = a + b; // legal

需要明确的是,您可以定义自己的类型,定义一个 + 运算符,在该运算符的方法内改变操作数之一(这可能会激怒您的消费者,无论是否可变类型),然后返回它,仍然不会使独立行a + b;合法。您必须在涉及上述表达式之一(赋值、调用等)的某物中使用其结果。

public class Foo
{
    public int Bar;

    public static Foo operator +(Foo a, Foo b)
    {
        a.Bar += b.Bar; // horrible mutation
        return new Foo() { Bar = a.Bar };
    }
}

///

Foo a = new Foo() { Bar = 1 };
Foo b = new Foo() { Bar = 2 };
a + b; // still not legal

Several answers mention immutability, and that's not what this is.

The line of code

a + b; 

Is simply not legal as a standalone statement. You will get the error

Only assignment, call, increment, decrement, and new object expressions can be used as a statement.

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.

a = a + b; // 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 line a + b; legal. You must use its result in something that is involved in one of the above expressions (assignment, call, etc.).

public class Foo
{
    public int Bar;

    public static Foo operator +(Foo a, Foo b)
    {
        a.Bar += b.Bar; // horrible mutation
        return new Foo() { Bar = a.Bar };
    }
}

///

Foo a = new Foo() { Bar = 1 };
Foo b = new Foo() { Bar = 2 };
a + b; // still not legal
囍孤女 2024-12-15 23:31:57

字符串是不可变的,这意味着字符串一旦创建就无法修改。但是,您可以创建一个新字符串,这正是 + 运算符应用于字符串时所做的操作。由于它创建一个新字符串并且不会修改现有字符串,因此您需要像其他答案提到的那样分配它。

这适用于所有不可变类型,例如整数,例如

int i = 2;
i + 2; //i is still 2
i = i + 2; //i is now 4
i += 2; //i is now 6

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.

int i = 2;
i + 2; //i is still 2
i = i + 2; //i is now 4
i += 2; //i is now 6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文