195

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)?

2
  • possible duplicate of Default access modifier in C# Commented Sep 21, 2010 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
    Commented Sep 21, 2010 at 20:25

4 Answers 4

299

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
4
  • 7
    A nice compilation from Reed's info, but you forgot to specify that such access modifiers apply to methods as well.
    – Joel
    Commented Jul 26, 2014 at 16:07
  • 7
    @Joel What do you mean? He clearly states "members". Members being data and behaviour and thus inclusive of methods.
    – rism
    Commented Feb 12, 2015 at 8: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
    Commented Mar 11, 2015 at 15:10
  • I'll add that a nested interface within a class is private, not internal, by default. Commented Aug 4, 2015 at 16:12
18

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)

12

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.

2
  • 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
    Commented Sep 27, 2012 at 14:57
  • 2
    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")] Commented May 17, 2018 at 16:13
0

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

5
  • 2
    aren't enums public by default? Commented Sep 21, 2010 at 19:14
  • 5
    @Jay: Unless they're nested. @Ryan: No, non-nested enums are internal by default.
    – Jon Skeet
    Commented Sep 21, 2010 at 19:18
  • 2
    @Ryan: Enum members are public by default, but the enum itself is internal. Commented Sep 21, 2010 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.
    – user5794376
    Commented May 15, 2016 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. Commented Nov 13, 2019 at 15:06

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