0

I create an ASP.NET web application running on .NET Framework 3.5. While writing code, I find that it does not throw error even I use language features which are not supported in .NET Framework 3.5.

This code is compiled successfully:

namespace WebApplication_3_5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string a = "World";
            string s = $"Hello {a}";
            dynamic b = "d";
        }

        public bool test => false;
    }
}

I am using Visual Studio 2022. What compiler is it using to compile this code with target framework .NET 3.5? Why does not it throw an error? Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Robin Sun
  • 1,352
  • 3
  • 20
  • 45
  • 2
    Maybe can refer here : https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version : NET Framework support until C# 7.3 – Fitri Halim Apr 13 '23 at 03:25
  • @FitriHalim But the target framework of this asp.net project is 3.5. It should not support syntax beyond C#3. Is it wrong? – Robin Sun Apr 13 '23 at 03:42
  • 2
    I don't think it correlates 1 to 1 like that. In fact the latest C# code now is C#11, but we have only .NET 7 as the maximum framework now. The languages and the framework are independent, so .NET framework 3.5 support up to C# 7.3 – Fitri Halim Apr 13 '23 at 03:47
  • 2
    Refer to this answer : https://stackoverflow.com/a/317885/13218799 – Fitri Halim Apr 13 '23 at 03:51
  • 3
    What Microsoft officially supports and what the compiler can do are quite different. Certain features require changes to the runtime to support them. Covariant returns are an example of this; prior to .NET 5, the runtime would consider it an invalid program and fail to load. Other features, like the target-typed `new` syntax, work as far back as .NET Framework 2.0, because nothing needed to be changed in the runtime to support the feature. – madreflection Apr 13 '23 at 03:56
  • 3
    Some features fall in the middle. Init-only properties require the `System.Runtime.CompilerServices.IsExternalInit` type, but the type doesn't have to exist in any particular assembly to be recognized, so you can provide your own (there's even [a package](https://www.nuget.org/packages/IsExternalInit) that does this for you). With the supporting type, the feature can be used with runtimes prior to what Microsoft officially supports it on. – madreflection Apr 13 '23 at 03:59
  • Try `dynamic b = new {};` which is also unsupported. – shingo Apr 13 '23 at 05:30
  • Newer syntaxes are a result of Roslyn compiler "add on" being installed. If you don't want to mess this up, or that your target server does not have (or can't use) the newer Roslyn compiler extensions, then remove the Roslyn compiler extensions from your project, and things like the $ etc. can't be used. Those compiler extensions crank our standard .net code, so it often does not matter what .net framework you have, since the compiler takes your source code and modifies it to standard source code, and THEN compiles the code. Hence, often the .net framework does not matter for new features – Albert D. Kallal Apr 13 '23 at 15:37

2 Answers2

1

Once upon a time the C# version matched the version of .Net runtime. But the runtime is kind of a pain to update since all users have to install the new framework. So Microsoft started to add more features to C# to make it more convenient for developers, while still targeting the same runtime, bumping the language version number in the process. This is possible since most language features only require a new compiler, not a new runtime.

So it is perfectly possible to use C# 7.3 while targeting the .Net framework 3.5 runtime. You only need a compatible compiler, and the compiler is part of visual studio.

In later releases Microsoft went back to one language version per runtime pattern. Probably since they now support bundling of the entire runtime with applications. Ofc, now the language version numbers no longer lines up with the .Net version numbers.

But the compiler is flexible enough to handle support on a feature by feature basis. So you can use the LangVersion flag in your project file to specify the language to use. Some language features will work, some will require features only available in newer runtimes and will not work, and some require workarounds to work. This flag is not officially supported, but I have not encountered any problems using it.

Ofc, if you are still targeting .Net 3.5 it is really time to update, going to 4.8 should be reasonably easy. Higher versions will require more work, but it will need to be done sooner or later if you want to continue supporting the application.

JonasH
  • 28,608
  • 2
  • 10
  • 23
1

The compiler used by Visual Studio 2022 in this case is CSC.

Usually, Visual Studio uses different compilers for different programming languages and different frameworks. But even the latest version of Visual Studio 2022 still has CSC built in. You can refer to C# compiler options.

And, C# language versioning, you will find that all versions of the .net framework support C# 7.3. It's as if Tianjin people can speak cross talk from birth. This explains why your program does not report an error.

enter image description here

sssr
  • 364
  • 1
  • 6
  • Hi @GetValue, thanks for your example of corss talk. Myself is in Tianjin. Now I know that Viusal Stuido uses Rosyln to compile C#. It does not use the compiler inside each framework, but the compiler which is part of Visual Studio installtion. C# in all version below 7.3 can be compiled by the same compiler, and the CLR of these framework can handle the dll. – Robin Sun Apr 16 '23 at 04:11