How to use new c# 8.0 features in Razor views

.net framework supports C# 7.3 that's why you can't make your Razor View work

.net core 3 supports C# 8 and i was able to make your example work with a .net Core 3 MVC app.

You can have a look here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version

I hope the above helps :)


OK so, there are some things that needs to be clarified first.

According to this answer C# language version is tied with the framework, and C# 8.0 is fully available to all language that support .Net Standard 2.1. .NET Framework 4.7.2 and below does not support .NET Standard 2.1 and it will not be the case in the future.

In the meantime there is a way to use C# 8.0 if you specify the LangVersion in any .csproj project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net48</TargetFrameworks>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>

This will enable C# 8.0 and some non-tied framework features will be available to you. Check the link answer to know which one.

Finally to answer my own question, at this time, no, there is no way to use any of the C# 8.0 features inside a Razor view. The run-time compilation seems to be done with a package named Microsoft.CodeDom.Providers.DotNetCompilerPlatform so the only way would be to update that package to allow some of the new features to be used.

I will update that answer as soon as I get more information.