In this post let's have a look at one of the newest additions to C#, which is the file access modifier.
For types that are declared with file access modifier, its visibility or scope is only limited to the file in which it's declared.
Consider the below code.
File1.cs
namespace MyNamespace; file class MyClass { public static void MyMethod() { // } }
File2.cs
namespace MyNamespace; file class MyClass { public static void MyMethod() { // } }
Here I have 2 classes with the same name and within the same namespace but in different files. With C# 11.0, this will build just fine. Prior to C# 11.0, we weren't able to do this.
file keyword can be applied only to the following types.
And it's allowed only at the top-level type.
public class MyClass { // Error: Not allowed as MyOtherClass is a nested type file class MyOtherClass { } }
It's very unlikely we will be using this file access modifier during business application development. But wanted to share it, because in case you see it, you know what it is.
More read:
file (C# Reference)
File-local types
Hope this helps.
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment