0

I want to develop C# with all possible warnings from Microsoft turned on, and I found this answer that says to add <Features>strict</Features> to my .csproj file.

However, before I start using it, I want to read up on what warnings it enables and other side effects it may have.

Unfortunately, I can't find any documentation on it whatsoever.

I assume it's in Microsoft's documentation somewhere, but I can't find it anywhere.

UPDATE: I found an article about this property with the following quote:

Note that this flag is not documented and not well-known.

Did Microsoft just never document this feature to begin with?

Floating Sunfish
  • 4,920
  • 5
  • 29
  • 48
  • 1
    This is not Microsoft documentation but It might helps https://www.meziantou.net/csharp-compiler-strict-mode.htm – Tony Stark Jun 14 '21 at 05:44
  • @TonyStark Ah, yes. I just added an update about this in the OP a few seconds ago. Sorry it didn't refresh for you before you posted this. Thanks for sharing! – Floating Sunfish Jun 14 '21 at 05:46
  • 1
    It's superseded by [Warnversion Warning Waves](https://github.com/dotnet/roslyn/blob/a6013f3213c902c0973b2d371c3007217d610533/docs/compilers/CSharp/Warnversion%20Warning%20Waves.md). You can set Level 5 (or 9999), which you can't do in the Project's options. – Jimi Jun 14 '21 at 06:23
  • 1
    See also: [C# Compiler Options to report errors and warnings](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings) – Jimi Jun 14 '21 at 06:37
  • Official [C# Compiler Errors and Warnings Level 1 to 5](https://learn.microsoft.com/dotnet/csharp/language-reference/compiler-messages/) –  Jun 14 '21 at 06:37
  • Thanks for the links, everyone. So in addition to `strict`, I should also add `9999` to my .csproj file, correct? This should probably turn on all warnings for my project. – Floating Sunfish Jun 14 '21 at 06:43
  • @Jimi Want to submit this as an answer so I can accept it? I will now add `9999` to my .csproj file in addition to `strict` to turn on all possible warnings. Since there isn't any documentation for the latter property, I think I should settle with this. Unless there's a better way to turn on all warnings in C#. – Floating Sunfish Jun 14 '21 at 06:59
  • 1
    If you use a new Compiler, then set `` (you cannot actually use this Level in older compilers: [Compiler Error CS1900](https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1900)), otherwise `strict`. If you really need it. -- It's probably more interesting if you post an answer yourself, so you can explain why you felt the need to enable this feature, what kind of issues it tries to solve and what kind of Project (Compiler version, .Net version) will be using it. – Jimi Jun 14 '21 at 07:23
  • 1
    As a note, the `strict` feature is used by Code Analysis / Profilers to issue warnings that are then filtered by the extension. You may get warnings that notify non-existent problems and you cannot get rid of them. – Jimi Jun 14 '21 at 07:32
  • @Jimi Alright, I'll post an answer myself once I finalize everything. Thanks! – Floating Sunfish Jun 14 '21 at 07:32

1 Answers1

1

Thanks to everyone who helped by posting links to useful articles and documentations.

For now, I have settled on creating a file named Directory.Build.props with the following content:

<Project>
  <PropertyGroup>
    <Features>strict</Features>
    <WarningLevel>9999</WarningLevel>
  </PropertyGroup>
</Project>

NOTE: For .NET Core, WarningLevel maxes out at 4. For .NET 5 and newer, it maxes out at 9999.

I will then copy and paste this file to the root directory of all my Solution files.

My goal was to have an easy way to enable all C# warnings for any project so I can code with confidence that I am not doing anything I'm not supposed to.

While I get most of my confidence from tests, each language has its own conventions and best practices which usually generate warnings when not followed, which was my motivation for enabling these features.

This approach should work with both Visual Studio Code and Visual Studio, and while I'm using the latest .NET SDK in my environment, it should work for all modern C# environments and any IDE as well.

Floating Sunfish
  • 4,920
  • 5
  • 29
  • 48