Using the file scope feature in .NET 7

Munib ButtSenior .NET/Azure Developer/Analyst Consultant
CERTIFIED EXPERT
Senior technical systems architect/ analyst and developer/mentor with over 30 years’ experience (20+ years in Canada)
Published:
Microsoft has just released .NET 7 on 14th November 2022. Today, we will look at a feature introduced with .NET 7 and that is the file access modifier.

The file access modifier keyword
 
 
Let us create a console application using Visual Studio 2022 Community edition.



Now, add a new class as below:



Next, add the below code to the new class:

namespace DotNet7File
{
    public class FileTest
    {
        public void PrintDateCall()
        {
             Console.WriteLine("Printed from the FileTest.cs file");
        }
      
    }
}

Then, add another class as below:


And add the below code to it:

namespace DotNet7File
{
    public class FileTest
    {
        public void PrintDateCall()
        {
             Console.WriteLine("Printed from the AnotherFileTest.cs file");
        }
 
    }
}


You will see the below errors:

 
The reason is that the same class has already been defined in this namespace. Now change the code in the second file to the below:

namespace DotNet7File
{
    file class FileTest
    {
        public void PrintDateCall()
        {
             Console.WriteLine("Printed from the AnotherFileTest.cs file");
        }
 
    }
}

All will compile fine. This is because the scope of the class in the second file has been changed to file and hence it is accessible at the file level only.
 
Add the below code to the “Program.cs” file:

using DotNet7File;
 
FileTest fileTest = new ();
 
fileTest.PrintDateCall();

Run the application and you will see the below output:


As expected, the class with the public modifier was created and called. So, how do we call the file level class. It can be called from within the file. Change your code in the second file as below:
 
namespace DotNet7File
{
    file class FileTest
    {
        public void PrintDateCall()
        {
             Console.WriteLine("Printed from the AnotherFileTest.cs file");
        }
    }
 
    public class ToCallExternally
    {
        public void CallTheFileLevelClass()
        {
             FileTest fileTest = new();
             fileTest.PrintDateCall();
        }
    }
 
}

And update the code in the Program.cs file as below:
 
using DotNet7File;
 
ToCallExternally toCallExternally = new ();
 
toCallExternally.CallTheFileLevelClass();

Now, run the application and you will see the below:


Here, we see the class at the file level is used.
 
Summary
In this article we looked at a new feature that has been introduced with .NET 7. The usage of the file modifier would probably be helpful is design of compiler related features in order to avoid conflict with user defined classes. In the next article, we will look into another feature of .NET 7.
0
426 Views
Munib ButtSenior .NET/Azure Developer/Analyst Consultant
CERTIFIED EXPERT
Senior technical systems architect/ analyst and developer/mentor with over 30 years’ experience (20+ years in Canada)

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.