-1
I see some programmers choosing to overwrite ToString
in C# among some cases, my doubt, it is a bad practice to override this in specific or even depends on the programmer decide?
Example:
public string FirstName { get; private set; }
public string LastName { get; private set; }
public override string ToString()
{
return $"{FirstName} {LastName}";
}
What do you mean by "method reserved"?
– Jéf Bueno
I improved/focused the question. Referring to whether it is a good or bad practice to override in Tostring or is it dev choice and situation.
– Raul Marciano
It depends, what is the purpose in overriding this method? There is the need even?
– Bruno Warmling
This code belongs to another person, in which I have no contact to know why this implementation and code posted is the whole class.
– Raul Marciano
In my view, it loses total coherence to overwrite it, for a simple
ToString
is not pointing out that the full name will be returned. However the concept of overwriting theToString
creates a dubiousness for me to know if there are cases where it can be a good practice or not.– Raul Marciano
Related: https://answall.com/q/212754/112052 <-- although in Java, the general idea is the same (but you can also see here, this yes specific to C#)
– hkotsubo