Search code examples
c#.netc#-7.0.net-4.6.1local-functions

Why can I write local functions in .NET Framework 4.6.1.?


Local functions were introduced in C# 7.0 and C# 7.0 came in .NET Framework version 4.7.

I'm absolutely certain that I'm running .NET Framework 4.6.1., but I can write and use local function without issue. How can this be explained?


Solution

  • See c# language versioning. All .net framework version uses C# 7.3 by default. Even if I suspect they mean all supported .net framework versions.

    All 4.* versions of .net uses the same runtime. Local functions are "syntactic suggar", and the only thing needed is a compiler that supports this feature, no runtime support is needed.

    You can specify <LangVersion>latest</LangVersion> in your project file and use language features from c# 11 if you want. This is not officially supported, but works well enough for me. Just note that not all features will be available, since some features require support from the newer runtimes.

    I would also consider updating to .Net 4.8, this should be fairly simple to do, and I do not think there is any supported windows build that includes 4.6.1 but not 4.8.