Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use new c# 8.0 features in Razor views

Tags:

I've updated my ASP.NET Mvc 5 web application to use the new c# 8.0 features through Visual Studio 2019 and everything works fine until I try to use these new features inside a Razor view.

For example, if I try to use the new switch expression:

@{
    ViewBag.Title = "About";

    var foo = 1;
    var bar = foo switch
    {
        1 => "one",
        2 => "two",
        _ => string.Empty
    };
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>

<p>Use this area to provide additional information.</p>

The compiler won't complain until I try to reach the page, giving me a compilation error.

Compilation error

I suspect that Microsoft.CodeDom.Providers.DotNetCompilerPlatform must be updated but it seems that there is no update available.

Is there any way to use c# 8.0 language features in Razor views?

like image 723
dbraillon Avatar asked Nov 07 '19 14:11

dbraillon


People also ask

What is the point of using new in C++?

Use of the new operator signifies a request for the memory allocation on the heap. If the sufficient memory is available, it initializes the memory and returns its address to the pointer variable. The new operator should only be used if the data object should remain in memory until delete is called.

Why new keyword is used in C?

new keyword The new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable.

What is :: operator new?

The operator new (or better the void* operator new(size_t) variant) just allocate memory, but does not do any object construction. The new keyword calls the operator new function, but then calls the object constructor.

What is new return C++?

Since new is a language keyword and not a function, it doesn't "return" anything.


1 Answers

.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 :)

like image 77
Ricky Stam Avatar answered Sep 17 '22 11:09

Ricky Stam