I want to know the difference between the public
and internal
visibility modifiers.
When should we use internal
on a class and
If you can Reference the assemble from outside , you have scope of Internal and public classes
Also, properties marked as internal
will throw a BindingExpression path error
if used for DataBinding in WPF. So those have to be public
in order to work properly, even when the DataBinding takes place within the same assembly.
public
is visible from wherever.
internal
is visible only within an assembly
You tend to use internal only to protect internal APIs. For example, you could expose several overloads of a method:
public int Add(int x, int y)
public int Add(int x,int y, int z)
Both of which call the internal method
internal int Add(int[] numbers)
You can then put a lot of sophistication on a method, but "protect" it using facade methods that may help the programmer to call the method correctly. (The implementation method with the array parameter may have an arbitrary limit of values, for example.)
Also worth noting that using Reflection, any and all methods are callable regardless of their visibility. Another "hack" to control/gain access to internally hidden APIs.
internal
is also useful when writing unit tests. The InternalsVisibleTo attribute let's your test assembly access internal methods in your code assembly. I.e. you can test methods that appear private to the outside world without using reflection.
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.
internal
is useful when you want to declare a member or type inside a DLL, not outside that...
normally, when you declare a member as Public
you can access that from other DLLs. but, if you needed to declare something to be public just inside your class library, you can declare it as Internal
.
in formal defenition: internal members are visible just inside the current assembly...