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.
– gato
@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
– Vinícius Lima