Skip to content

Commit 4c158be

Browse files
authored
Add more clarity on member lookup (#43932)
Found while updating the feature spec for the feature. This article didn't clearly state that file local types are preferred over other types with the same name in the file where they are declared. Also, the `file` modifier doesn't actually modify the *scope*, rather, it modifies the *visibility*. Update the language to match the speclet definition. Finally, quick edit pass.
1 parent 6217577 commit 4c158be

File tree

1 file changed

+9
-9
lines changed
  • docs/csharp/language-reference/keywords

1 file changed

+9
-9
lines changed

docs/csharp/language-reference/keywords/file.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
2-
description: "file modifier: Declare types whose scope is the file in which it's declared"
3-
title: "file keyword"
4-
ms.date: 09/15/2022
2+
description: "The file modifier: Declare types whose visibility is the file in which it's declared"
3+
title: "The file keyword"
4+
ms.date: 12/10/2024
55
f1_keywords:
66
- "file_CSharpKeyword"
77
helpviewer_keywords:
88
- "file keyword [C#]"
99
---
10-
# file (C# Reference)
10+
# The file modifier
1111

1212
Beginning with C# 11, the `file` contextual keyword is a type modifier.
1313

14-
The `file` modifier restricts a top-level type's scope and visibility to the file in which it's declared. The `file` modifier will generally be applied to types written by a source generator. File-local types provide source generators with a convenient way to avoid name collisions among generated types. The `file` modifier declares a file-local type, as in this example:
14+
The `file` modifier restricts a top-level type's visibility to the file in which it's declared. The `file` modifier is most often applied to types written by a source generator. File-local types provide source generators with a convenient way to avoid name collisions among generated types. The `file` modifier declares a file-local type, as in this example:
1515

1616
```csharp
1717
file class HiddenWidget
@@ -20,11 +20,9 @@ file class HiddenWidget
2020
}
2121
```
2222

23-
Any types nested within a file-local type are also only visible within the file in which it's declared. Other types in an assembly may use the same name as a file-local type. Because the file-local type is visible only in the file where it's declared, these types don't create a naming collision.
23+
Any types nested within a file-local type are also only visible within the file in which it's declared. Other types in an assembly can use the same name as a file-local type. Because the file-local type is visible only in the file where it's declared, these types don't create a naming collision.
2424

25-
A file-local type can't be the return type or parameter type of any member that is more visible than `file` scope. A file-local type can't be a field member of a type that has greater visibility than `file` scope. However, a more visible type may implicitly implement a file-local interface type. The type can also [explicitly implement](../../programming-guide/interfaces/explicit-interface-implementation.md) a file-local interface but explicit implementations can only be used within the `file` scope.
26-
27-
## Example
25+
A file-local type can't be the return type or parameter type of any member declared in a non file-local type. A file-local type can't be a field member of a non-file-local. However, a more visible type can implicitly implement a file-local interface type. The type can also [explicitly implement](../../programming-guide/interfaces/explicit-interface-implementation.md) a file-local interface but explicit implementations can only be used within the same file.
2826

2927
The following example shows a public type that uses a file-local type to provide a worker method. In addition, the public type implements a file-local interface implicitly:
3028

@@ -34,6 +32,8 @@ In another source file, you can declare types that have the same names as the fi
3432

3533
:::code language="csharp" source="./snippets/Program.cs" id="ShadowsFileScopedType":::
3634

35+
Member lookup prefers a file-local type declared in the same file over a non-file-local type declared in a different file. This rule ensures that a source generator can rely on member lookup resolving to a file-local type without ambiguity with other type declarations. In the preceding example, all uses of `HiddenWidget` in *File1.cs* resolve to the file-local type declared in *File1.cs*. The file-local declaration of `HiddenWidget` hides the public declaration in *File2.cs*.
36+
3737
## C# language specification
3838

3939
For more information, see [Declared accessibility](~/_csharpstandard/standard/basic-concepts.md#752-declared-accessibility) in the [C# Language Specification](~/_csharpstandard/standard/README.md), and the [C# 11 - File local types](~/_csharplang/proposals/csharp-11.0/file-local-types.md) feature specification.

0 commit comments

Comments
 (0)