Home > Back-end >  Why is "file" considered a keyword in C#?
Why is "file" considered a keyword in C#?

Time:09-14

The following code generates CS9056 ("Types and aliases cannot be named 'file'"):

public sealed class file
{

}

This happens after I upgraded VS to 2022 Preview, 17.4.0. The code used to compile just fine previously. I cannot find any information indicating that file was now a keyword in C#. The solution I'm trying to translate builds using the 5.0SDK but targets .NET Framework 4.8.

I do know that types should use uppercase names normally and that I could work around the issue by changing class file to class @file, but this is in an auto-generated file where I cannot change the generator. And applying some sed hacks seems like, well, a hack.

Why is file considered a keyword here and what would it be intended for? Can I disable it?

CodePudding user response:

This is a change introduced in the compiler in VS 2022 17.4. file is apparently now a modifier for type declarations.

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/breaking-changes/compiler breaking changes - dotnet

Types cannot be named file Introduced in Visual Studio 2022 version 17.4. Starting in C# 11, types cannot be named file. The compiler will report an error on all such type names. To work around this, the type name and all usages must be escaped with an @

...

You can learn more about this change in the associated csharplang issue.

Additional link referenced from docs: https://github.com/dotnet/csharplang/issues/6011

  • Related