128

I want to know the difference between the public and internal visibility modifiers.

When should we use internal on a class and when public? I am confused with when a method should be public or internal.

I read that internal can be accessed through the assembly, while public can also be used through the assembly. Then where does the difference lie?

0

8 Answers 8

140

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.

5
  • 5
    in a internal class can i declare the public methods..? Commented Nov 15, 2010 at 7:18
  • No, as this would cause a conflict. The compiler will tell you of this and fail to compile.
    – Program.X
    Commented Nov 15, 2010 at 7:37
  • 17
    @Program.X: Actually, you can and the compiler doesn't tell you anything. Commented May 11, 2014 at 17:38
  • Actually I came here googling whether or not this is actually a feature. Imagine making the class internal and all methods public. If you need higher visibility, you can set the whole class to public again. But I am not sure, whether or not it is meant to be used this way. So I continue googling... edit Et voilà, same topic: stackoverflow.com/questions/9302236/…
    – ecth
    Commented Feb 3, 2017 at 7:53
  • You can make the internal methods visible to a different assembly by adding this line in the AssemblyInfo.cs file: [assembly:InternalsVisibleTo("MyOtherProject")] Commented Feb 14, 2023 at 13:17
47

Internal is useful when you want to declare a member or type inside a DLL, and not outside of that.

Normally, when you declare a member as Public, you can access that from other DLLs. But, if you need to declare something to be public just inside your class library, you can declare it as Internal.

Formal definition: Internal members are visible just inside the current assembly.

21

internal is also useful when writing unit tests. The InternalsVisibleTo attribute lets your test assembly access internal methods in your code assembly. That is, you can test methods that appear private to the outside world without using reflection.

1
  • 1
    Thanks a lot! I was doing a search about this because unit tests can't reach the internal methods.
    – Tahirhan
    Commented Nov 21, 2022 at 12:48
6

Public can also be accessed outside of the assembly. So when you have a class that shouldn't be accessed every class in the assembly should be able to access it, then internal is the right thing. If you need outside access, use public.

1
  • Some examples of practical side-effects or problems from using "public" instead of "internal" for a typical shop might be helpful. For smallish apps, one doesn't typically divide app source code up into multiple assemblies. While libraries may be in a different assembly, those can usually snoop anyhow via reflection even for "internal" items, I believe.
    – FloverOwe
    Commented Jan 27, 2023 at 0:33
5

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.

4

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.

0

In a very simple language:

Internal : you will only able to access within a assembly. for ex: if you have created a AccountService.csproj with internal class

public interface IAccount{

  int TotalAmount(long accountID);
}
internal class Account:IAccount{
  
  public int TotalAmount(long accountID){
       ...
   }
}
public class Customer{
   
 public long accountID {get;set;}
 public int GetTotalAmount(){
   IAccount account = new Account();
   return account.TotalAmount(accountID)
 }
}

if you are referring AccountService.csProj to BankService.csProj (BankService.csProj --> AccountService.csproj)

Below are the properties that accessible in BankService.csProj    
Customer.GetTotalAmount() -- Accessible
IAccount.TotalAmount()    -- Accessible
Account.TotalAmount()     -- Not Accessible (as account class is internal)
-4

If you can Reference the assemble from outside , you have scope of Internal and public classes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.