I have a project that targets older versions of the .NET framework (.NET 4.5.2). I installed Visual Studio 2015 (and therefore .NET 4.6 on my machine). I noticed that if I use C# language features released in .NET 4.6/C# 6, it still compiles. If my project's target framework is < .NET 4.6, shouldn't this not compile:
public string MyExpressionBodyProperty => "1";
//auto properties are new in C# 6
public string MyAutoProperty { get; } = "1";
private static void MethodThatUsesNameOf(string filename)
{
if (filename == null)
{
//nameof was released in C# 6
throw new ArgumentException("The file does not exist.", nameof(filename));
}
}
How can I ensure I'm only using .NET language features that work with the framework version I'm targeting?