New File Scope Feature In .NET 7

Introduction

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.

Some more articles on new features of .Net 7 are listed below:

The file access modifier keyword

Let us create a console application using Visual Studio 2022 Community edition.

New file scope feature in .NET 7

New file scope feature in .NET 7

Now, add a new class as below,

New file scope feature in .NET 7

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,

New file scope feature in .NET 7

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,

New file scope feature in .NET 7

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,

New file scope feature in .NET 7

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,

New file scope feature in .NET 7

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


Recommended Free Ebook
Similar Articles