176
votes

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

4
possible duplicate of Default access modifier in C#Jeff Sternal
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

4 Answers

279
votes

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
15
votes

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

Default Nested Member Accessibility & Allowed Accessibility Modifiers

Source: Accessibility Levels (C# Reference) (December 6th, 2017)

9
votes

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.

0
votes

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