188

I'm trying to find a reference for the default visibility of various aspects of C#. Class types, fields, methods, enums, etc.

Can someone provide a list of these along with their default visibility (i.e., no prefixed modifier)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan Peters
  • 7,608
  • 8
  • 41
  • 57
  • possible duplicate of [Default access modifier in C#](http://stackoverflow.com/questions/3675575/default-access-modifier-in-c) – Jeff Sternal Sep 21 '10 at 19:18
  • 10
    I wouldn't consider it a duplicate... that question is specific (What's the default for THIS?), this one is broad (What are ALL defaults?) – WernerCD Sep 21 '10 at 20:25

4 Answers4

295

All of the information you are looking for can be found here and here (thanks Reed Copsey):

From the first link:

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

...

The access level for class members and struct members, including nested classes and structs, is private by default.

...

interfaces default to internal access.

...

Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.


From the second link:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.

And for nested types:

Members of    Default member accessibility
----------    ----------------------------
enum          public
class         private
interface     public
struct        private
Community
  • 1
  • 1
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 7
    A nice compilation from Reed's info, but you forgot to specify that such access modifiers apply to methods as well. – Joel Jul 26 '14 at 16:07
  • 7
    @Joel What do you mean? He clearly states "members". Members being data and behaviour and thus inclusive of methods. – rism Feb 12 '15 at 08:27
  • 1
    In case any vb.net developers are looking at this, vb is different. The default scope for vb class members is `Public`. `Enum` members are also `Public`. Probably less confusing to just use explicit scope in your case since chances are high it will be translated or read by c# developers at some point. The code generation in vb.net does not do this for you, unfortunately. – toddmo Mar 11 '15 at 15:10
  • I'll add that a nested interface within a class is private, not internal, by default. – Frank Bryce Aug 04 '15 at 16:12
19

From MSDN:

Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.


Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.

Members of Default member accessibility Allowed declared accessibility of the member
enum public None
class private public
protected
internal
private
protected internal
private protected
interface public public
protected
internal
private*
protected internal
private protected
struct private public
internal
private

* An interface member with private accessibility must have a default implementation.

Source: Accessibility Levels (C# Reference) (September 15th, 2021)

N3pp
  • 33
  • 1
  • 4
Nicholas Miller
  • 4,205
  • 2
  • 39
  • 62
11

By default, the access modifier for a class is internal. That means to say, a class is accessible within the same assembly. But if we want the class to be accessed from other assemblies then it has to be made public.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mrk
  • 151
  • 1
  • 4
  • 23
    This information was already present in the other answers. You should only answer a question, especially an old one such as this, when you have additional information to provide or you think that the other answers are wrong. Anyway, welcome to Stack Overflow. – Gorpik Sep 27 '12 at 14:57
  • 1
    A class does not necessarily need to be made public to be made accessible to other assemblies. One could use the InternalsVisibleToAttribute Class `[assembly:InternalsVisibleTo("Friend1b")]` – Adam Rodriguez May 17 '18 at 16:13
0

By default is private. Unless they're nested, classes are internal.

Svisstack
  • 16,203
  • 6
  • 66
  • 100
  • 2
    aren't enums public by default? – Ryan Peters Sep 21 '10 at 19:14
  • 5
    @Jay: Unless they're nested. @Ryan: No, non-nested enums are internal by default. – Jon Skeet Sep 21 '10 at 19:18
  • 2
    @Ryan: Enum members are public by default, but the enum itself is internal. – Reed Copsey Sep 21 '10 at 19:20
  • @ReedCopsey But the book I'm learning from clearly says that `enum members are private by default, so to use them outside the enum we should declare them as public`. Can you please explain why the book contradicts with what you say? Thanks. –  May 15 '16 at 15:26
  • Whatever book @user5794376 (clearly a deleted account) is reading should be given all 1-star reviews if what they say is in the book is true. It only takes looking at Microsoft Docs to know that enum members are public by default and is why it is common practice for enums only to not be explicit on them. – Rodney S. Foley Nov 13 '19 at 15:06