is it possible to disable implicit ToString() call?
There is no way to prevent this code at compile time. Object.ToString
is a part of the public contract of every object and there are no ways to prevent it from being invoked at compile time. In this particular case the compiler will resolve the +
to String.Concat(object, object)
and the implementation ends up invoking Object.ToString
. There is no way to change this. I think your smoothest path forward is to override ToString
and have it call into FormatAddress
Please do not change ToString
to throw an exception as a few others are suggesting. The majority of .Net expects that ToString
exists and is non-throwing. Changing that will have many unexpected negative side effects to your program (including killing the debug experience for those objects)
You can override ToString in your customer class and within there you can call the FormatAddress method if needed.
public override string ToString()
{
return FormatAddress();
}