Introduction
Consider this simple (and bad) C# class:
using System;
namespace N
{
static class C
{
static void M(DateTime d)
{
if (d == null)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
static void L(object o)
{
if (o is Nullable)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
}
Both methods M
and L
have serious issues.
In M
, we ask if a value of the non-nullable struct DateTime
is equal to null via the lifted ==
operator (which exists since DateTime
overloads operator ==
). This is always falls, and the compiler can tell at compile-time, so we have a branch ("Yes"
) which is unreachable.
In N
we ask if o
is an instance of the static class Nullable
which can never be the case (note, the static class Nullable
is not the same as the struct Nullable<>
). Again, this is a developer mistake, and the "Yes"
statement is unreachable.
We do want a compile-time warning (or "warning as error") in these cases, right?
As it seems, through gradual accumulation of compiler errors and/or omissions in the old C# compiler that was used for C# 1.0 through 5.0, the expected compile-time warnings failed to appear with the old compiler. Luckily we have Roslyn/C# 6.0/Visual Studio 2015 now, and expect to get a warning. But no, because of the desire to not emit warnings from Roslyn that where not present with the old compiler (backwards compatibility?), these situations are still not warned against.
However, if you compile from the command line, with csc.exe
, you can use:
csc.exe /features:strict ... ...
and you will get the warnings you want! /features:strict
makes csc.exe
include warnings that the old C# compiler "fogot".
My question
How do I specify the equivalent of /features:strict
to msbuild.exe
command line or in the .csproj
file?
Sometimes, e.g. when we have XAML in our build project, it is not easy to use csc.exe
directly, we have to use a .csproj
file and compile through msbuild.exe
.
See Question&Answers more detail:
os