The suggested duplicate is indeed a similar question. However, the answer there covers only one option - disabling the ToString() itself. There are other possible solutions such as having Visual Studio warn me about it, or have the ToString() not be called (read the answer there carefully, he assumes it is called, and just explains that there is no way of "removing" ToString().), or use a lower version of C# (did it always work this way?), etc...
When I have a non string somewhere where a string is expected by the code I don't want it automatically converted to string. I want an error. For example:
public string Stringify()
{
    return Data + Environment.NewLine;
}
And Data is a byte[] I DON'T want to get:
System.Byte[]
I want to get an error so I know I need to fix it. (There's a reason for strong typing.)
So, is there a way to get Visual-Studio/C#/.Net to show me an error/throw-an-Exception when using non strings as strings?
Rosyln analyzers can be installed in your project using Nuget in Visual Studio 2017.
In particular there is a Rosyln analyzer that checks for implicit and explicit calls to ToString() if ToString() is not overridden in the class. https://www.nuget.org/packages/ToStringWithoutOverrideAnalyzer/
Installed analyzers can be found under references

By default this analyzer will produce warnings as shown by the triangle with !, but right clicking on the rules you can elevate any rule to be an error, or reduce it to informational.
Console.Write and Console.WriteLine have several overloads including Console.Write(object value), so if you are printing some object that hasn't overridden ToString() like Console.Write(myCustomObject) the analyzer will not catch this because there is no implicit conversion being made (at least in the code that you have written)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With