2

I noticed that in C# that the + operator takes a string on the left and an object on the right:

string output = "" + numbers.ElementAt(0); 
//vs
string output = numbers.ElementAt(0).ToString();

Why is the default behavior to accept an object on the right and not a string? When I look other + operators for say int, it is strongly typed for int/int instead of int/object.

I know in C# all objects have the ToString() method, is this just a way to remove the need to call .ToString() in the first example above?

0

3 Answers 3

3

Why is the default behavior to accept an object on the right and not a string?

Well, first off, there is an overload that accepts two strings, and if you pass two strings that's the overload that will be used, so it is the default behavior. But if you pass a string and a non-string, then the (string, object) overload will be used.

As for why that overload is there, it's because the C# language designers felt that it was entirely sensible to concat a string to any object, and had a reasonable implementation for doing so for any type of object.

When I look other + operators for say int, it is strongly typed for int/int instead of int/object.

How would you implement a operator +(int, object) overload? I don't see any good implementation here, unlike with string concatenation.

I know in C# all objects have the ToString() method, is this just a way to remove the need to call .ToString() in the first example above?

It actually works if the object is null, whereas your solution does not. If you would account for that then yes, that is essentially all that the overload is doing.

Sign up to request clarification or add additional context in comments.

Comments

0

is this just a way to remove the need to call .ToString() in the first example above

Yes.

Comments

0

String concatenation automatically transforms non-strings into strings.

Source: C# Language Specification: Addition operator, MSDN.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.