In general, public
methods should meet very high standards for robustness (not crashing or corrupting data due to bad input) and security awareness (not allowing unexpected input to trigger an exploit). But for internal
, protected
, and private
methods, it will often be reasonable to follow more relaxed standards, since one has full control over what inputs each method can receive.
Because parameters passed to a public
method (possibly from an external source) are considered less trustworthy than parameters received from within one's own assembly, methods marked as public
are often treated differently by code analyzers than otherwise identical methods marked as internal
. Just as an example, with a public
method the analyzer may warn you to check that the method's parameters are not null. With internal
methods, it may be possible to configure the analyzer to be less strict about null
checking. Or the analyzer may be able to determine on its own, by doing flow analysis of all the source files for the assembly, that null
will never be passed to the particular method as an argument, and thus determine there is no need to check if the parameter is null
. There are many other examples of analyzers treating public
and internal
methods differently.
By correctly marking classes, methods, properties, fields, interfaces, etc. with the correct access modifiers, you correctly signal to code analyzers your intent, and then in return the analyzer can give you more relevant warning messages and advice.