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.