Disadvantages of coding in C#6.0 but compiling against .NET 2.0

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();
}
Jon Skeet
people
quotationmark

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:

  • LINQ unless you provide your own implementation
  • Expression trees
  • Extension methods unless you add your own attribute
  • FormattableString for interpolated strings (minor; you can still use interpolated strings as strings)
  • Dynamic typing
  • Generic variance
  • Caller info unless you add your own attributes
  • Async/await (not sure how feasible it would be to provide your own implementation of this... pretty major job)

Other features such as lambda expressions, expression-bodied members, anonymous types etc should work fine.

people

See more on this question at Stackoverflow