Stringbuilder append() converts to string automatically

Asked

Viewed 40 times

3

Why when I use .AppendLine() it is necessary to convert the parameter to string, but when use .Append() conversion is not necessary?

1 answer

1

Let’s see the documentation of AppendLine() and of Append(), which by the way is what should always be done before using any language/technology resource adopted and its Apis.

There it shows that the second method has several overloads dealing with many different data types like argument. And when none is satisfied directly at least the object is adopted, which is the parent type of all. And falling into this method he makes a conversion to string internally.

The first method accepts only one data that is already string (has an option where it has not given some to pass).

There’s a better chance of something going wrong in the Append() since an implicit conversion may not give the expected result and may only be cyst during execution in certain situations. An early conversion gives you a little more control than you want. It doesn’t mean it’s more certain, but you have to consider this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.