none
To use a high-levelled syntax with a lower net framework and compiler, it can compile successfully, why? RRS feed

  • Question

  • Hi all,

    You know that C# now comes to 7.1, with a lot of new syntaxes and features (called "Syntax Sugers").

    But these sugers CANNOT BE EATEN in the previous versions such as VS2013, VS2012 or VS2010. If you copy the source codes and put them there, obviously there must be lots of mistakes because compilers there cannot recognize them. And for VS2010 (net framework 4), For VS2013 (net framework 4.5)……All in all, I think most of time C# or vb.net's syntax goes with the version of net framework (Not direct relationship but something trackable).

    Next to my interesting question:Since VS2013 (net framework 4.5) or VS2010 (net framework 4.0), we don't have C# 7.1 yet. Generally speaking, if we write C# 7.1's syntaxes there should be errors to be compatible with the compiler at that time. But the latest VS WON'T do that——This means I can write with the latest syntaxes in a lower net framework (compiler). Why? A typical example is that I can write C# 7.1 syntaxes with the framework down to 4.0, which is the highest version in VS2010. However the codes cannot be compiled there successfully in VS2010....

    Can anyone give me some explainations and tell me what the advantages of that?

    Reguards!


    Reproduce your quesions with   ScreenToGif is your choice.
    For IIS: IIS Forum,
    For WebSite of .NET, ASP.NET Forum,
    For others, StackExchange
    For spam-sender or forum urgent issues, Send your Email at: forumsfeedback@microsoft.com



    Saturday, August 19, 2017 1:09 PM

All replies

  • By design of the .NET Framework compilation and run process, the sourcecode langauge and langauge version are irrelevant. This was literally one of the design goals of the system. Possibly a core one.

    The targeted .NET Version defines how the MSIL can look. Wich classes it can reference. Aside from the classes you can use, it mostly maters for the Compiler and Runtime. It is a behind the scene thing. You pick the minimum version with all the classes you need.

    The C# version the compiler supports is only relevant for parsing your input. As you said, that is Syntax Sugar. It is entirely a compiler side modificaiton. i++ looks (propably) the same in MSIL as i = i +1. Because "i = i + 1" is what i++ respresents.

    As long as my Compiler can read the C# 7.1 Syntax, I can use 7.1 Syntax - even if my target is the 1.0 Framework. There are some limits to this, of course. Syntax sugar that relates to REGEX of course requires the REGEX classes to run it.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Saturday, August 19, 2017 7:27 PM