Warning CS0108 trips when a variable is declared with the same name as a variable in a base class.
Warning CS0114 is the equivalent for methods.
Both flag up as a warning but not an error. Compilation continues and the executable can be run.
But the risk is a logic error or bug where the wrong one is invoked.
This is extremely dangerous in live production environments.
It's also very "non-obvious" when debugging: the code looks like it's doing the right thing when stepped through but is not. The bug can take forever to identify in heavily abstracted/inherited code-bases.
Surely the "new" keyword exists for a reason and (even more surely) usage should be enforced by a compile error - not just a warning.
But this is not so. You have to set it up by hand on each project in a solution; it's not even possible to enforce solution-wide.
And I haven't yet found a way to make this the default behaviour for new solutions.
Why?
You can actually set the warnings to be errors solution wide (kind of). Only restriction is that the solution has to be in the top level directory.
You can add a Directory.Build.targets file next to your solution to set up the warnings to be treated as errors. This file will automagically be loaded and used in the build process (see more in the docs: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2019#directorybuildprops-and-directorybuildtargets)
<Project>
<PropertyGroup>
<WarningsAsErrors>$(WarningsAsErrors);CS0108;CS0114</WarningsAsErrors>
</PropertyGroup>
</Project>
As for why it is by default a warning and not an error (my thoughts): errors are things that break the build and you can't compile the project/solution, while warnings are to warn the user about a potential mistake.
Visual Studio by default considers as an error everything that makes a program unable to compile. Since these considerations do not affect the compilation but only the quality of the code, they are considered warnings. But as Styxxy has pointed out, you can treat any warning you like as an error.
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