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.