0

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. Different VS may have different kinds of compilers.

Why don't we have multiple compilers for us to switch if we wanna make our codes "compatible" with different compilers in different kinds of VS (e.g: If I wanna write a plain of codes suitable for VS2010. I firstly switched my compiler to that of VS2010 and do writing). That'd be better?

xqMogvKW
  • 628
  • 7
  • 17
  • 1
    Because is the compiler that reads your code and compile it. The Framework is not involved in this. If you compile using VS2017 then you don't have many problems with these "Syntactic Sugar" expressions. _C# or vb.net's **syntax** goes with the version of net framework_ is plain wrong – Steve Aug 19 '17 at 13:22
  • @Steve:Since different kinds of compilers have different kinds of syntaxes. Why don't we have different compilers nested for a VS products so we can know whether what we wrote "compatible" in a lower compiler to pass? – xqMogvKW Aug 19 '17 at 14:03
  • As far as I know you can have different versions of VS (and thus compiler) on the same machine. – Steve Aug 19 '17 at 14:11
  • But you do have it. Project > Properties > Build tab > Advanced button > Language version. – Hans Passant Aug 19 '17 at 15:59

1 Answers1

1

Original question

To use a high-levelled syntax with a lower net framework and compiler in the latest VS, it can compile successfully, why?

Matt Warren's blog entry is a great read on this issue, as is Eric Lippert's.

A common technique along the way though is to have the compiler “lower” from high-level language features to low-level language features in the same language.

In short, the 'newer' features are converted ('lowered') to 'older' features - so they continue to run on the framework (even though it has no notion of the newer feature).

The Roslyn source code for using is an example of lowering (where it converts using into try finally):

/// <summary>
/// Rewrite a using statement into a try finally statement.  Two forms are possible:
///   1) using (expr) stmt
///   2) using (C c = expr) stmt
///   
/// The former is handled by RewriteExpressionUsingStatement and the latter is handled by
/// RewriteDeclarationUsingStatement (called in a loop, once for each local declared).
/// </summary>

Bonus question

Why don't we have multiple compilers for us to switch if we wanna make our codes "compatible" with different compilers in different kinds of VS (e.g: If I wanna write a plain of codes suitable for VS2010. I firstly switched my compiler to that of VS2010 and do writing). That'd be better?

You can easily do this, as long as you are on VS 2013 or later. The C# compiler is available standalone from VS 2013 onwards.

Additionally, you don't generally need to do this, since Visual Studio allows you to specify the compiler version.

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • In fact what I really want to ask is what I've changed above (And for the beginning, I didn't make myself clear). But some of your answers have given me suggetions to deeply understand "Lowering". So "+1" is voted for you. – xqMogvKW Aug 19 '17 at 14:02
  • Check out `Bonus question` above @xqMogvKW . – mjwills Aug 19 '17 at 23:02