Target.Net 4.5 in VS2015, but am I allowed to use C # 6 functions?

I am working in VS2015 on a project that targets the .NET Framework 4.5

In the build settings for the language version is set to "default"

As I read, C # 6 is only added in .Net 4.6, but I allow (at least to compile and run the code) the function string interpolationI read is C # 6.

Now I'm confused: now I'm compiling for C # 6 / .Net 4.6 or for .Net 4.5 (and how can I check this?)

EDIT

In the comments, I see that the syntax of C # 6 has nothing to do with the version of the .NET framework. I got this idea from this answer ( What are the correct version numbers for C #? ), Which says that 'C # 6.0 is released with .NET 4.6 and VS2015 (July 2015). 'so I realized that C # 6 (somehow) is connected with .NET 4.6

+4
source share
2 answers

C # 6 functions, such as string interpolation, are compiler functions, not runtime (CLR) functions. Therefore, you can use them no matter what version of .NET you are creating against if your compiler supports C # 6.

Visual Studio 2015 , , Properties = > Build tab = > Advanced button = > Language Version

+7

, # .NET Framework. # Framework, . , - .

, # 5.0 async - await .Net 4.0, Microsoft.Bcl.Async.

, # 6.0 , . :

string s = $"pie is {3.14}";
Console.WriteLine(s);

:

string s = string.Format("pie is {0}", 3.14);
Console.WriteLine(s);

.Net 4.5.

, , IFormattable FormattableString. , :

IFormattable formattable = $"pie is {3.14}";
Console.WriteLine(formattable.ToString(null, CultureInfo.InvariantCulture));
Console.WriteLine(formattable.ToString(null, new CultureInfo("cs-CZ")));

:

IFormattable formattable = FormattableStringFactory.Create("pie is {0}", 3.14);
Console.WriteLine(formattable.ToString(null, CultureInfo.InvariantCulture));
Console.WriteLine(formattable.ToString(null, new CultureInfo("cs-CZ")));

.Net 4.6, .Net 4.5

CS0518: 'System.Runtime.CompilerServices.FormattableStringFactory'

, :

namespace System.Runtime.CompilerServices
{
    class FormattableStringFactory
    {
        public static IFormattable Create(string format, params object[] args) => null;
    }
}

, NullReferenceException.

, StringInterpolationBridge .

+3

Source: https://habr.com/ru/post/1653650/


All Articles