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.
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?
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.
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.
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.
Since new is a language keyword and not a function, it doesn't "return" anything.
.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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With