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()
ifToString()
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.Potential gotcha
Console.Write
andConsole.WriteLine
have several overloads includingConsole.Write(object value)
, so if you are printing some object that hasn't overriddenToString()
likeConsole.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)When you have a
string
type and use the+
operator
then it will call thevirtual ToString()
method for any type that isn't astring
automatically.You can't avoid this by just adding types to produce a
string
(at least not in a simple or practical manner).You can do something else instead; such as
If you want to make sure
Data
is always astring
without testing then I would pass it to the method as a parameter.And the obvious case; if you want
Data
to be a field and always astring
is to declareData
as astring
to start with.Edit:
As stated in comments above; I now understand that you want to basically flag any use of
virtual ToString()
calls to prevent operations like this from happening. A comment was given to update the Roslyn Analyzer and a link to where that's already been done before. I'm not posting those comments here to steal the answer but I do agree that, that may be the best solution for what you want. Another option would be to use reflection but that wouldn't work until runtime.You can explicitly try cast Data and VS will complain if its not a string