This question popped into my head today.
C#6.0 or any other version for that matter all introduce cool new functionality making things easier to code.
But lets say I have a client running WindowsXP which doesnt support .NET 4.6.
Are they any downsides to writing my code in C#6.0 (or whatever the latest C# version is in the future) but compiling it against the .NET 2.0 framework (or some other version of .NET which is different than the C# version that it was released with)
EDIT
For those who are unaware you can target the .NET Framework via Project Properties -> Application tab -> Target Framework
and you can target the C# Language Version via Project Properties -> Build tab -> Advanced -> Language Version.
EDIT2 - C#6.0 Code Compiled on .NET 2.0 and run on WindowsXP
static void Main(string[] args)
{
try
{
StringBuilder sb = null;
string value = sb?.ToString(); // C#6 Feature. Wont throw an exception.
if(value == null)
{
Console.WriteLine("The Value is null");
}
string value2 = sb.ToString(); // Will cause an "Object Reference not set to an instance" exception
}
catch ( Exception ex) when (ex.Message.Contains("object")) // C#6.0 conditional catches
{
Console.WriteLine("Exception Caught");
}
catch(Exception ex)
{
Console.WriteLine("Other Exception");
}
Console.ReadLine();
}
Are they any downsides to writing my code in C#6.0 (or whatever the latest C# version is in the future) but compiling it against the .NET 2.0 framework (or some other version of .NET which is different than the C# version that it was released with)
Only that there will be features you can't use:
FormattableString
for interpolated strings (minor; you can still use interpolated strings as strings)Other features such as lambda expressions, expression-bodied members, anonymous types etc should work fine.
See more on this question at Stackoverflow