C# 11 - File Scoped type


With C# 11, we got a file keyword, which acts like an access modifier. The class with file type is only limited within the file where it is defined. The feature is mainly for source generators because the source generator would have to scan other people’s assemblies to try and find a name that doesn’t clash with what the user already has in their assembly or other assemblies. So for that reason, now they have the file keyword, which they can use and not worry about it by scanning everyone’s code which can also lead to faster source generators. 

File Scoped Type

Assume you have two classes, Customer and Customer1. As both classes use the same name, we will get a compile error, as shown below.

File Scoped type

When you change the class type to file 

File Scoped type

No errors, now the scope of the class is limited within this file. 


We have seen the feature of file keyword from C# 11. The scope of the file type and the purpose of using the file keyword. We will see more about C# 11 features in my next blog. 

Happy Coding!!!