What's the difference between internal and public in C#?


(Yassine) #1

I just need some full answer of internal and public, I already know the basic idea behind both of them… but really what makes internal any different from public!!! I know internal will make the member or entity only accessible within the package itself (.dll or .exe) and not from any external package. but other than that what are the other common uses of internal, and what really makes it used instead of public.

(danny rok) #2

Hi @Yassine,
You needed to know, what are the differences between internal and public classes in C#?
Well here is how I see it
internal is visible only in an assembly.
What happens when you declare a member as Public? is that you will have the ability to access from other DLLs.
But, if you needed to declare a member to be public just inside your class library, you can declare it as Internal.
as I said, internal members are visible only inside the current assembly.
Try to use internal only when you want to protect the internal APIs, you have the ability to expose several overloads of a method, by example.
public int Add(int x, int y)
public int Add(int x,int y, int z)
Both of them requires an internal method
internal int Add(int[] numbers)

After that you can put a lot of affectation by a method, but, to help the programmer to connect to the method correctly, it is necessary to “protect” it using some false appearance methods, by example (The execution method with the parameter set may have an arbitrary limit of the values).

Warning: using the reflection, each and every method is callable regardless of its seen. Considered as Another “hack” to control/gain access to internally hidden APIs.
And remember, if you used properties marked as internal for DataBinding in WPF, you will receive a BindingExpression path error.
That’s why you should make be public in order to work properly if the DataBinding takes place within the same assembly or not in both cases this will work