Why add a variable, property, method, etc. in a string does it work?

2
When I add a variable, property, method, enumerator, etc. in a string , it works, even though I do not call the ToString() method 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
}
  • Why does this happen, does the compiler automatically convert the type to string ? How does this work?
  • Why does the enumerator pass the field name instead of the value?
  • If I do not perform the conversion manually (with a .ToString() for example) can it have any performance differences or lead to a future problem?
  • Regarding best practices, what would be the right way to do it?
asked by anonymous 13.02.2018 / 19:49

1 answer

2
  

Why does this happen, does the compiler perform the conversion from type to string automatically? How does this work?

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

  

Why does the enumerator pass the field name instead of the value?

Because they have chosen this way, what matters in the enumerator is their name and not their value. They could have used value, but it made less sense. This is a setting for Enum .

  

If I do not perform the conversion manually (with a .ToString () for example) can it have any performance differences or lead to a future problem?

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

The first one is called only if it already receives a string and operates directly on the received value, but if it does not have this signature obviously it will not make a difference because only the other form can be called.

The second one will be called if it is another type, unless it has 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 where the conversion takes place. The problem occurs in types by value because there will be a boxing ( more info ) and an instance by reference will be created and the value copied to pass as Object This has huge cost of processing and memory. If the type is already by reference there is no extra cost.

  

In relation to good practices, what would be the correct way to do it?

The one that is correct in the context that will be used. I leave the framework to turn around because in most cases the performance is not as important and the cleaner code becomes more readable.

    
13.02.2018 / 21:00