20

Is it possible to use Roslyn compiler and new features of C# 6.0 with old versions of .NET Runtime (for example, .NET 4.0)?

For example, I want use the expression-bodied members (int S => x + y; instead of int S { get { return x + y; } }) in .NET 4.0 application.

AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
  • 1
    What would make you think it would be? – George Stocker Jun 26 '14 at 17:00
  • How would a compiler *compile* a method it was unaware of (because it didn't exist when it was created) to [IL](http://en.wikipedia.org/wiki/Common_Intermediate_Language)? – Erik Philips Jun 26 '14 at 17:03
  • 12
    It's relatively likely that it could, since this could be totally implemented via compiler support and emit IL that the 4.0 CLR can run. Just like you can use Automatic Properties in a project targeting .Net 2.0 if you compile with the C# 3.0 compiler. http://stackoverflow.com/questions/9393982/why-are-my-auto-implemented-properties-working-in-asp-net-2-0 – shf301 Jun 26 '14 at 17:25
  • Have you tried it? Primary constructors work for me for a project targeting .NET 3.5. – Mike Zboray Jun 26 '14 at 17:26
  • 7
    @GeorgeStocker What would make you think it would not be? Unless the feature needs to be backed by changes in the .NET runtime (like async/await), there is no reason that it should not be usable on old .NET runtimes. – jakobbotsch Jun 26 '14 at 18:12
  • These are purely syntactic features. – SLaks Jun 26 '14 at 18:42

2 Answers2

14

The new C# 6.0 features don't depend upon framework support so yes, you an app compiled with the C# 6.0 compiler will run on .NET 4.0, assuming of course you specify you are targeting .NET 4.0 in your project file in the first place.

Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • has this changed at all since June? We'll still be able to use the new compiler with .net 4.0? – DLeh Nov 21 '14 at 20:26
  • 1
    Still true, and at this point C# 6.0 is fairly locked down so it's unlikely to change. – Jason Malinowski Nov 21 '14 at 22:23
  • 1
    thanks for the update. couldn't find this info elsewhere. Our customer still needs XP support, I'm glad to see that we'll be able to upgrade to Roslyn – DLeh Nov 21 '14 at 23:15
  • 1
    Even if C# 6.0 _did_ have ties to a newer version of the framework, the compiler still supports a flag for compiling to older versions of the language. So you wouldn't get to use the language features in this hypothetical situation, but would still get the "other" benefits of Roslyn. – Jason Malinowski Nov 24 '14 at 15:47
  • The answer gives impression C# compiler has no problem running on .NET 4.0, when in fact it is not a supported platform. – Oleg Mihailik Sep 02 '15 at 07:45
  • The question was asking not about the compiler, but about whether apps can down-target. I've revised my answer text to hopefully make it a bit less ambiguous. – Jason Malinowski Sep 02 '15 at 18:10
  • There is at least one exception to the answer. If your app uses DynamicMethod.CreateDelegate(type), when compiled with c# 6, it will not run on framework 4.0. The exception is "Method not found: 'System.Delegate System.Reflection.MethodInfo.CreateDelegate(System.Type)" – Elroy Flynn Jan 18 '16 at 16:56
  • Does your project file actually specify you are targeting .NET 4.0, though? If you still targeted a later version in your project, all bets are off regardless. – Jason Malinowski Jan 18 '16 at 20:47
1

As of now Roslyn C# compiler and tooling cannot run on .NET 4.0

You can certainly cross-compile for .NET 4.0, but to run the compiler itself you need at least .NET 4.5

Oleg Mihailik
  • 2,514
  • 2
  • 19
  • 32