Compiler Warning CS8981

Discovering the new C#11 compiler warning CS8981

Home DailyDrop

Daily Knowledge Drop

C# 11 introduced a new compiler warning CS8981, which is raised when a type is declared consisting of only lowercase letters.

The warning

Considering the following class declaration:

public class myclass
{
}

When using C# 11, this will raise the following warning:

The type name 'myclass' only contains lower-cased ascii characters. 
    Such names may become reserved for the language.

The code will still compiled and execute, but the compiler is warning the developer about potential future issues.


The reason

There are a couple of reasons for the warning (and I am sure other's I have not mentioned here):

  • Encourages best practices for class declarations, which should be declared using PascalCase as a default
  • As explicitly mentioned in the warning, it prevents potential future conflicts with potential reserved language names - this allows future versions of the language to include new keywords, limiting the impact on existing applications

Notes

A small, almost inconsequential language feature - but personally I am big fan of these kinds of initiatives, and hope to see more in future versions. They help guide developers down the "pit of success" path (which not forcing it outright), and they also allow the developers of the language to enhance and grow it, with limited naming conflicts.


References

The Best C# 11 Feature You Don’t Need

Daily Drop 215: 02-12-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net warning compiler