8

I've recently downloaded VS2017 and have been trying out some of the new C#7 features. However, one is confusing me because it doesn't always work.

Local Functions...

Are these supposed to work in an MVC Web Application? I've created a brand new WPF Desktop application and it works, but when I create a brand new MVC Web Application, the following code doesn't work (yes, I know it's pointless).

public ActionResult Index()
{
    void TestMethod(string message)
    {
        Console.WriteLine(message);
    }

    TestMethod("Why isn't this working?");

    return View();
}

The IDE doesn't show an error, but when I compile it, it produces a list of errors:

HomeController.cs(13,13,13,17): error CS1547: Keyword 'void' cannot be used in this context
HomeController.cs(13,28,13,43): error CS1528: Expected; or = (cannot specify constructor arguments in declaration)
HomeController.cs(13,28,13,29): error CS1003: Syntax error, '[' expected
HomeController.cs(13,29,13,35): error CS1525: Invalid expression term 'string'
HomeController.cs(13,36,13,43): error CS1026: ) expected
HomeController.cs(13,36,13,43): error CS1003: Syntax error, ',' expected
HomeController.cs(13,43,13,44): error CS1003: Syntax error, ']' expected
HomeController.cs(13,43,13,44): error CS1002: ; expected
HomeController.cs(13,43,13,44): error CS1513: } expected
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Am I doing something wrong?

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • Yes... you've declared another method inside your controllers method for returning the Index view. Move it above. – Wheels73 Mar 30 '17 at 09:42
  • 5
    But that's what Local Functions are meant to allow you to do... – spydacarnage Mar 30 '17 at 09:43
  • @Wheels73, that's part of C#7 - it should allow it. OP, what .net framework are you using? – Ori Nachum Mar 30 '17 at 09:44
  • Ori Nachum.. Oh right... thanks for that.. I clearly didn't read that in OP's post! – Wheels73 Mar 30 '17 at 09:45
  • I get the same issue, oddly in a console App I can select C# 7.0 as the IOS language, in Project, Properties, Build, Advanced. But in a ASP.NET Web Application, I get a build error when selecting it: "CS1617 Invalid option '7' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. WebApplication1" – DaveShaw Mar 30 '17 at 09:48
  • 1
    Which led me to this: http://stackoverflow.com/questions/41839097/inline-variable-declaration-not-compiling – DaveShaw Mar 30 '17 at 09:51
  • 1
    Does http://stackoverflow.com/questions/42844341/using-c-sharp-7-features-inside-of-a-view-in-an-asp-net-mvc-core-project help at all? – David Arno Mar 30 '17 at 09:52
  • Or http://stackoverflow.com/questions/42744689/enabling-c-sharp-7-in-a-asp-net-application might help too. – David Arno Mar 30 '17 at 09:53
  • Thanks DaveShaw! Updating Microsoft.Net.Compilers to v2 did the trick! – spydacarnage Mar 30 '17 at 10:15

0 Answers0