C# File Access Modifier: Syntax and Use Cases
Understand how the C# file access modifier restricts a top-level type to its source file, with syntax examples and comparisons to internal, public, and private.

Introduced in C# 11, the file modifier restricts a top-level type to the source file where it is declared. It fills a gap between private and internal: a type can be shared by several types in the same file without being visible anywhere else in the assembly. C# already had public, private, protected, internal, protected internal, and private protected for visibility control, but none of them provided file-level scope.
What Does file Actually Restrict?
The file modifier can be applied to top-level types: classes, records, structs, interfaces, enums, and delegates. It cannot be applied to nested types, members, or local functions. A file-local type is visible only within the source file where it is declared. This is stricter than internal, which allows visibility throughout the same assembly.
// FileA.cs
file class Helper
{
public static int Compute(int x) => x * 2;
}
public class Processor
{
public int Process(int value) => Helper.Compute(value);
}In this example, Helper is only accessible from FileA.cs. If another file in the same project tries to reference Helper, the compiler raises an error because Helper is not visible outside its file. The file modifier prevents naming collisions when multiple files in a large project define helper types with the same name.
How file Compares to internal
internal makes a type visible to all code within the same assembly. file limits visibility to the single source file. This distinction matters when you have a helper type used by several types in one file but you do not want it to become part of the assembly-wide internal API.
| Modifier | Scope | Use Case |
|---|---|---|
public | Any code that can reference the assembly | Public API of a library |
internal | All code in the same assembly | Internal helpers shared across the project |
file | Only the source file that defines the type | One-file helpers, avoiding name collisions |
private | Only the containing type | Implementation details of a class |
When you have a helper class used only by one other class in a single file, file prevents accidental external use. internal would expose it to the whole assembly, which might be more than you need. The choice depends on whether the helper is conceptually part of the assembly's internal contract or purely a local implementation detail.
Applying file to Different Type Kinds
The file modifier works on all top-level type declarations, but you must place it before the type keyword.
file struct Point
{
public int X { get; set; }
public int Y { get; set; }
}
file interface ILogger
{
void Log(string message);
}
file enum Severity { Low, Medium, High }
file delegate void AlertHandler(string message);All of these are valid. You cannot combine file with public, internal, protected, or private; it defines the visibility by itself. Attempting file internal class produces a compile-time error.
Practical Use: File-Local Records
A common pattern is to define a small record or class that is only used as an intermediary in a specific file. Without file, you might pollute the namespace with a type that only one method uses. With file, you can keep the declaration close to its usage.
// In File.cs
file record UserProfile(string Username, int Age);
public class ProfileService
{
public string GetDisplayName(int id)
{
UserProfile profile = LoadProfile(id);
return $"{profile.Username} ({profile.Age})";
}
private UserProfile LoadProfile(int id) => new UserProfile("alice", 30);
}Because UserProfile is file-scoped, another file cannot accidentally reference it. This makes the codebase easier to reason about: if you see a file type, you know you only need to understand that single file to see all its usages. A private method can use a file type as a return type because its accessibility is narrower than the type's file-level visibility.
File-Scoped Namespaces: A Related but Different Feature
C# 10 introduced file-scoped namespaces, which allow you to write namespace MyApp; at the top of a file instead of wrapping every type in braces. That feature changes how namespaces are declared, not how types are visible. It is easy to confuse the two because both involve files. File-scoped namespaces are a syntactic convenience; the file modifier is a visibility control.
When file Can Cause Problems
Because file types are visible only within their file, they cannot be exposed by public members of a type that is visible beyond that file. If a public method in FileA.cs returns a file type, callers outside that file could not name or use the returned type, so the compiler rejects the declaration.
// FileA.cs
file class InternalHelper { }
public class Exposer
{
// Error: Inconsistent accessibility
public InternalHelper GetHelper() => new InternalHelper();
}The compiler enforces that a member's accessibility cannot exceed that of its parameter and return types. Since InternalHelper is file, it is less accessible than public, so the method cannot expose it. In a type visible beyond the file, internal members have the same problem. If you need to return a helper from a public method, the helper must be public; from an internal method, it must be at least internal. If the helper is used only inside a single class, a private nested class may be a better alternative.
Compatibility and Tooling Considerations
file requires C# 11 or later, which means you need at least the .NET 7 SDK or a compiler that supports C# 11 language features. Most modern projects can enable this without issue. However, if you are working on an older codebase or using a tool that compiles with an earlier compiler, file is not available. You can use internal or private nested classes in those cases, though you lose the file-level restriction.
Another operational point: reflection can still see file types, but they are emitted with internal accessibility. Tools such as source generators may need to avoid emitting references to file-local types, because generated code in another file cannot reference them.
Maintainability Tradeoffs
Using file keeps large files self-contained. When you open a file with several helper types, you immediately know they are not shared. That reduces the risk of unintended coupling across the assembly. However, if the helper becomes useful in another file, you must change its modifier to internal or public, which is a deliberate refactoring step. That is generally good: it forces you to think about whether you want to expose the type beyond its file.
On the other hand, overusing file can make a project harder to navigate because types are not discoverable through IntelliSense outside their file. A developer searching for a type name in another file will not see it, which could be confusing if they expect it to be available. Use file for truly local helpers, not for types that might naturally be reused later.
Final Decision Guidance
When you have a type that is only used within one source file, file is the precise modifier. When the type is used by multiple files within the same assembly, use internal. When you are building a library and want the type visible to consumers, use public. You might also consider private nested classes if the type is only used inside one class and you prefer not to have a top-level type at all. file sits between private and internal in scope, offering a level of encapsulation that aligns with C#'s emphasis on explicit visibility control.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.