Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default visibility for C# classes and members (fields, methods, etc.)?

Tags:

c#

People also ask

What is __ attribute __ (( visibility default?

Summary. To summarize, __attribute__((visibility("default"))) is often used in a shared library, and expected to be compiled with -fvisibility=hidden (though it doesn't have to). Symbols with "default" visibility will always be public outside of the shared library itself.

What is __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

What does Fvisibility hidden do?

-fvisibility=hidden makes all your symbols hidden by default. What you then have to do, is choose which functions you want to be visible to users linking against your library and make them visible by marking them with a visible attribute.


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

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.

Default Nested Member Accessibility & Allowed Accessibility Modifiers

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


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.