m.edmondson
m.edmondson

Reputation: 30882

Append to string

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?

Upvotes: 2

Views: 1238

Answers (6)

Anthony Pegram
Anthony Pegram

Reputation: 126864

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

Upvotes: 1

Guffa
Guffa

Reputation: 700342

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;

Upvotes: 3

David Ly
David Ly

Reputation: 31586

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

Upvotes: 1

Jon
Jon

Reputation: 437376

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.

Upvotes: 2

Joe
Joe

Reputation: 82604

Needs assignment

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

or

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

Upvotes: 7

Uwe Keim
Uwe Keim

Reputation: 40736

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.

Upvotes: 2

Related Questions