Is there a way to generate a warning if the return value is not checked?

We have our own API written in C #, where many of the methods have return values, usually bool or ints, which sometimes ignore callers. The return values ​​exist for some reason, for example, to indicate a problem or result of some kind, so we want to encourage callers to actually check the result, and not just blithely assume that the method worked properly, only to make things worse adrift.

Is there a way to use the Visual Studio compiler to force verification of return values ​​by flagging calls using a warning or error when the caller cannot check the return value of an invisible method?

+5
source share
2 answers

If you want to encourage best practices, use exceptions. This, at least, will make them realize that something is happening if they simply neglect something. If they bury an exception, they bury it. But this requires an active solution.

Basically, every time your code can go β€œI’m at the point where there is a problem, and the solution is either absent or ambiguous” - this is when to quit.

Aside from throwing exceptions when you return control back to the caller, you can do nothing programmatically to ensure that they then do it.

+4
source

A warning compiler already exists for this, [MSDN] [1]

[1]: https://msdn.microsoft.com/en-us/library/ms182273.aspx you are using FX cop

+2
source

All Articles