481

What is the difference between the internal and private access modifiers in C#?

Jim Fell
  • 13,750
  • 36
  • 127
  • 202

7 Answers7

601

internal is for assembly scope (i.e. only accessible from code in the same .exe or .dll)

private is for class scope (i.e. accessible only from code in the same class).

explorer
  • 11,710
  • 5
  • 32
  • 39
  • 8
    i have a quick question; if i declare a class as private in a namespace within an assembly versus the class being internal, i can access that class within the assembly in both cases, then what is the difference between a private class and an internal class? or is private and internal modifiers in terms of classes used when nesting class within class? – mayotic Apr 11 '12 at 22:06
  • 2
    i added the specific project/assembly as a reference of another project and both private as well as internal prevent me from accessing this particular class within the namespace, so then again... difference between private and internal classes is? thanks – mayotic Apr 11 '12 at 22:27
  • 12
    You cannot declare a top level class as private. The compiler will stop you. – TheGateKeeper Apr 12 '12 at 09:12
  • 7
    @NetSkay: If you declare a private class inside a public class then this class is not accessible by other classes in your assembly but if you declare this class as internal then it would be accessible in the assembly. Although, they both will not be accessible outside the assembly. – Yogesh Jindal Jul 06 '12 at 15:36
  • 2
    If you are coming from a VB.NET background, C#'s "internal" keyword is equivalent to VB.NET's "Friend" keyword. – Brain2000 Apr 24 '14 at 16:59
  • 1
    I guess my question would be why would you want to do this? Why would you want to provide an internal instead of a public? – Demodave Apr 19 '18 at 16:21
249

Find an explanation below. You can check this link for more details - http://web.archive.org/web/20230209015322/https://www.dotnetbull.com/2013/10/public-protected-private-internal-access-modifier-in-c.html

Private: - Private members are only accessible within the own type (Own class).

Internal: - Internal member are accessible only within the assembly by inheritance (its derived type) or by instance of class.

enter image description here

Reference :

dotnetbull - what is access modifier in c#

Community
  • 1
  • 1
Vivek
  • 5,025
  • 2
  • 18
  • 9
  • 24
    Interesting that `Protected Internal` provides wider access than `Protected`. – Dan Bechard Mar 16 '16 at 14:44
  • 11
    @Dan, thoroughly agree. It might be helpful to read and think of `protected internal` as`protected OR internal`. – chessofnerd Sep 09 '16 at 14:35
  • 8
    What does `With Type` mean? – James Wierzba May 08 '17 at 18:30
  • @Dan yeah, i assumed it would just act as a truth table and `AND` that matrix to true|false|false|false for `protected internal`. not intuitive. instead it's true|true|true|false as if `OR'd`. – ferr May 29 '17 at 21:49
  • 1
    Incidentally, C# 7.2 just added a "protected AND internal" modifier, though the actual keywords chosen, `private protected`, aren't very intuitive. For details: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private-protected – Joe Sewell Nov 16 '17 at 19:11
  • @JamesWierzba I assume `With Type` refers to accessing the member from an instantiated object of the class, which happens outside of the class. Notice how `With Type` is X for private and protected. – Matthew Eskolin Oct 01 '21 at 15:14
79

internal members are visible to all code in the assembly they are declared in.
(And to other assemblies referenced using the [InternalsVisibleTo] attribute)

private members are visible only to the declaring class. (including nested classes)

An outer (non-nested) class cannot be declared private, as there is no containing scope to make it private to.

To answer the question you forgot to ask, protected members are like private members, but are also visible in all classes that inherit the declaring type. (But only on an expression of at least the type of the current class)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
10

internal members are accessible within the assembly (only accessible in the same project)

private members are accessible within the same class

Example for Beginners

There are 2 projects in a solution (Project1, Project2) and Project1 has a reference to Project2.

  • Public method written in Project2 will be accessible in Project2 and the Project1
  • Internal method written in Project2 will be accessible in Project2 only but not in Project1
  • private method written in class1 of Project2 will only be accessible to the same class. It will neither be accessible in other classes of Project 2 not in Project 1.
Salik
  • 458
  • 7
  • 14
9

private - encapsulations in class/scope/struct ect'.

internal - encapsulation in assemblies.

Yaniv Lugassy
  • 91
  • 1
  • 2
9

Private members are accessible only within the body of the class or the struct in which they are declared.

Internal types or members are accessible only within files in the same assembly

Jesper Fyhr Knudsen
  • 7,802
  • 2
  • 35
  • 46
4

Internal will allow you to reference, say, a Data Access static class (for thread safety) between multiple business logic classes, while not subscribing them to inherit that class/trip over each other in connection pools, and to ultimately avoid allowing a DAL class to promote access at the public level. This has countless backings in design and best practices.

Entity Framework makes good use of this type of access

Nathan Teague
  • 825
  • 6
  • 11