Home > OS >  Is it possible to transpile C# version 10 or 11 code to older versions of the C# language?
Is it possible to transpile C# version 10 or 11 code to older versions of the C# language?

Time:11-08

Is it possible to transpile C# language version 10 or 11 source code to older versions of the C# language? Just like there are transpilers for Javascript to port code written in newer versions of Javascript to older versions of Javascript.

CodePudding user response:

Nothing pre-rolled. Some features can be represented in down-level languages -and some tools offer similar features - for example, IIRC "Reflector" allowed you to specify the language version when decompiling IL, but: decompiled code can also frequently include things that can't actually be represented in pure C#, and: not all up-level features can be represented in down-level C#. Simple features like simple properties: sure, but: they're not hard to do manually anyway.

In most cases, however, you can use an up-level C# version on a down-level project, by using the <LangVersion> element in the csproj; for example, <LangVersion>10</LangVersion> or <LangVersion>latest</LangVersion>. This is probably the better route to explore here. Some language features demand runtime support - which makes them framework-version dependent; some language features just require specific types to exist, and will work if you define those types locally or import them as a package (for example, Microsoft.Bcl.AsyncInterfaces).

CodePudding user response:

No. But that is not the important question.

C# code is compiled to CIL code, that is executed by the runtime. Some language features are "syntactic sugar" and can be used on any runtime. These can typically be used by just setting the langversion to "latest" in the project file.

Other features, like the new "generic math", require new runtime features, so it cannot be used with older runtimes. So if you want to use the new features you would essentially have to bundle the entire runtime with your application, and that is already one of the ways .Net core applications can be deployed. So just create a self contained deployment bundle.

  • Related