Microsoft has just released .NET 7 on 14th November 2022. In the previous article, we looked at improvements in string literals. Today, we will look at another feature introduced with .NET 7 and that is the file access modifier. Let us begin.

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.

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 in the 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.

HostForLIFE.eu ASP.NET Core Hosting

European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.