Default Access Modifiers in C# OOP
Here is a list of default access modifiers on different C# objects .
Internal
- Classes and Structs:
internal
access modifiers are used by default if no access modifier is supplied when defining a class or struct. This indicates that only within the same assembly may the class or struct be accessed. - Interfaces : Their default access modifier is
internal
if none is given when it is defined. This indicates that only users of the same assembly can access the interface. - Delegates: The default access modifier when defining a delegate type is also
internal
. This indicates that only members of the same assembly may access the delegates. - Fields and Methods:
internal
is the default value for any field or method that is specified inside of a class or struct without an access modifier. - Properties: If a property is declared without an access modifier, it defaults to
internal
.
Public
In C#, there is only one member with a default access modifier of public
, and that is the constructor. If no access modifier is specified for a constructor, it defaults to public
, meaning it can be accessed from anywhere in the code, both within the same assembly and in other assemblies.
Implemented methods inside a class that are originating from an Interface has a default modifier of public
.
Private
- Fields: A field’s default access level is
private
if it is declared inside of a class or struct without an access modifier. The field can only be accessed from inside the same class or struct, according to this statement. - Methods: Methods are declared inside one without an access modifier, it is assumed to be
private
. This indicates that a method is exclusively accessible from within a single class or struct. - Properties: A property’s default access level is
private
if it is declared inside of a class or struct without an access modifier. This indicates that a property is exclusively accessible from within a single class or struct. - Nested Types: If a nested class, struct, interface or enum is declared within a class or struct without an access modifier, it also defaults to
private
. This means that the nested type can only be accessed within the same class or struct.
Protected
There are no members in C# with a default access modifier of protected
.
Static
There are no members in C# with a default access modifier of static
.
Sealed
static
classes are sealed
by default. Other than that, there are no objects having sealed
modifier by default.
Abstract
There are no objects in C# with a default access modifier of abstract
.
Partial
There are no objects in C# with a default partial
modifier.
Private Protected
There are no objects in C# with a default Private Protected
modifier.
Protected internal
There are no objects in C# with a default Protected Internal
modifier.
Important!
Interfaces can not have default access modifiers other than public
. But remember that there is “Explicit interface implementation”, which effectively makes the members of the interface inaccessible from outside the implementing class or struct like private
.
Interfaces in C# do not have a default access modifier. When declaring an interface, you must explicitly specify its access modifier. This means that an interface can be declared as public
, internal
, protected
, or private
, depending on the needs of your application.
A Comment or a clap is much appreciated! Thank you for your time!
My some other relevant stories:
Dynamic Memory Allocation VS Static Memory Allocation Common
Difference between “Field” and “Property” in C# OOP
Common Collection Interfaces in C#: A Quick Summary
Singleton Class & Design Pattern in C# OOP
Static Class vs Singleton Class in C# OOP