C# Default Access Modifiers: internal or private?
In C#, if you omit access modifiers when declaring class
, struct
, or field
, the compiler applies a default. Understanding what these defaults are helps you avoid unintentional access issues in your code.
Default for class and struct: internal
When you declare a class or struct without specifying an access modifier, C# considers it internal
by default. That means it is accessible only within the same project.
// Default is internal
struct MyStruct { }
class MyClass { }
What does “same project” mean?
Each project in a solution compiles into its own .dll
or .exe
, known as an assembly. The internal
keyword means that other projects—even in the same solution—cannot access the type.
- Same project: can access internal types
- Different project in same solution: cannot access internal types
Example project structure
MySolution/
├── ProjectA ← declares internal struct
└── ProjectB ← references ProjectA
// In ProjectA
struct MyStruct { } // treated as internal
Here, MyStruct
is usable in ProjectA but not in ProjectB, even though both belong to the same solution.
Default for fields: private
Fields inside a class or struct default to private
if no access modifier is given. That means they are only accessible from inside the type itself.
struct MyStruct
{
int x; // private by default
int y; // also private
}
Fields are private by default — remember this when designing public APIs or exposing data.
Summary
class
andstruct
default tointernal
field
defaults toprivate
internal
= only visible inside the same project (assembly)
While you can rely on defaults, it's a good habit to explicitly specify access modifiers for clarity and maintainability.