Why does adding a variable, property, method, etc. to a string work?

Asked

Viewed 104 times

2

When I add a variable, property, method, enumerator, etc. into a string, it works, even though I’m not calling the method ToString() for example.

Example with type and integer:

var example = typeof(bool);
var exampleInteger = 12;

Console.WriteLine("Resultado: " + example); // Resultado: System.Boolean
Console.WriteLine("Resultado: " + exampleInteger); // Resultado: 12

Example with enumerator:

public enum Example {
    AnyField,
    Haha,
    Huehue,
    Popotao
}

public static void Main()
{
    var example = Example.Haha;

    Console.WriteLine("Resultado: " + example); // Resultado: Haha
    Console.WriteLine("Resultado: " + Example.Popotao); // Resultado: Popotao
}
  • Because this happens, the compiler performs type conversion to string automatically? How does it work?
  • Because the enumerator passes the name of the field instead of the value?
  • If I don’t perform the conversion manually (with a .ToString() for example) may have some difference in performance or lead to a future problem?
  • In terms of good practice, what would be the right way to do?
  • I didn’t quite understand what your question about strings is.

  • @cat It’s really like the question, I wanted to know why this occurred, if the system did the conversion automatically, and if to leave it to him to do it could lead to loss of performance or other problems. : D

1 answer

2


Because this happens, the compiler performs type conversion to string automatically? How does it work?

Since all types have a textual representation, even if it does not produce the expected result the type system managed by the compiler ensures that the type is automatically converted to string whenever he is the most suitable in that position. This is called coercion or implicit casting.

Why the enumerator passes the name of the field instead of the value?

Because they chose so, after all what matters in the enumerator is its name and not its value. They could have used the value, but it made less sense. That’s a definition of Enum.

If I don’t perform the conversion manually (with a .ToString() for example) may have some difference in performance or lead to a future problem?

Usually there is a small gain if you do it manually because it is common the method that expects the value to have a signature with string and another with object.

The first is only called if you already receive one string and operates directly on the amount received, but if you don’t have that signature it obviously won’t make a difference because only the other way can be called.

The second will be called if it is another type, unless you have a signature with the specific type, which is rare. The conversion will be done internally. It may seem that the cost is the same and only changed from where the conversion is made. The problem occurs in types per value because there will be a Boxing (more information) and an instance by reference will be created and the value copied to pass as Object This has huge processing cost and memory. If the type is already by reference there is no extra cost.

In terms of good practice, what would be the right way to do?

The one that is correct in the context you use. I leave the framework turn around because in most cases performance is not so important and the cleaner code becomes more readable.

  • When you say first it would be string and second object?

  • 1

    @Vinicius.

  • I did not understand where the conversion to string.

  • 1

    @Cat In question for example, I concaten a string with a int. In the end everything doesn’t turn out like one string correct? @Maniero replied: "the type system managed by the compiler ensures that the type is automatically converted to string".

  • @Viníciuslima this to speak of the concatenation is not "Resultado: " + exampleInteger;? In this case the conversion occurs at this time. It is that you implied that you wanted to manipulate the variables as a string.

Browser other questions tagged

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