38

I have an Asp.Net MVC App running with framework version .NET 4.5 and I'm using VS2017 pro version. Users can upload attachments including but not limited to:

  • Excel
  • Word
  • PowerPoint
  • pdf
  • jpeg
  • png

So I have a private function which is as follows:

private string ImageExtension(string path)
{
  string noImagePath = HttpContext.Current.Server.MapPath("~/Content/images/Template/No-Image-Available-100x100.jpg");
  string fileExtension = System.IO.Path.GetExtension(path);
  switch (fileExtension)
  {
    case ".jpg":
    case ".jpeg":
    case ".gif":
    case ".png":
       return path;
    default:
       return noImagePath;
   }
}

As you can see it's very simple and does not do anything fancy. As I'm only using this in one place I thought about making the use of new C# 7 feature of local function. So I've gone ahead and created my main function as follows and added ImageExtension(string path) inside it.

public void BugInfo(HttpPostedFileBase file)
{
  if(file != null && file.ContentLength > 0)
  {
    string fileName = file.FileName;
    string directoryPath = "directory path";

     //save path of 
     string savePath = System.IO.Path.Combine(directoryPath, fileName);
     string testString = ImageExtension(savePath);

     string ImageExtension(string path)
     {
        string noImagePath = HttpContext.Current.Server.MapPath("~/Content/images/Template/No-Image-Available-100x100.jpg");
        string fileExtension = System.IO.Path.GetExtension(path);
        switch (fileExtension)
        {
          case ".jpg":
          case ".jpeg":
          case ".gif":
          case ".png":
             return path;
          default:
             return noImagePath;
         }
      }
    }
  //save values to db here
 }

With the above code my project builds without any errors. As soon as I hit F5 or Ctrl + F5 I get the following error screen

enter image description here

If I check in the ErrorList to see if there are any errors I get none at all as you can see below.

enter image description here

Can someone tell me where I'm going wrong please. Do I have to change any settings or need to include any additional DLLs to make use of C# 7 features.

Looking at this answer it seems like all of the C# 7 features should work on .NET 4.5

Community
  • 1
  • 1
Izzy
  • 6,740
  • 7
  • 40
  • 84
  • 7
    Try looking in the Output pane/window, select Compilation in the combo box. Perhaps the error message has been recorded there. – xanatos Apr 03 '17 at 08:32
  • 2
    As well as what @xanatos said, there's a dropdown at the top of the error list pane that lets you choose different sources for display there too (build/intellisense). – James Thorpe Apr 03 '17 at 08:35
  • @xanatos Thanks, i'll have a look and see if that helps. – Izzy Apr 03 '17 at 08:37
  • @JamesThorpe I've got two dropdowns which one would you like me to change? – Izzy Apr 03 '17 at 08:38
  • @Downvoter please let me know how I can improve this question? – Izzy Oct 22 '18 at 06:47

1 Answers1

47

You need to update nuget package named "Microsoft.Net.Compilers" to the latest version. Most likely you have version 1.3.2 installed in your project but you need 2.0.1 to use C# 7 features. Alternatively - you can remove this package at all (together with packages that depend on it) - then it will also work, because it will use your installed compiler, but I don't recommend doing that.

As this package description says:

.Net Compilers package. Referencing this package will cause the project to be built using the specific version of the C# and Visual Basic compilers contained in the package, as opposed to any system installed version.

So that is why it uses C# 6 compiler for you now.

Evk
  • 98,527
  • 8
  • 141
  • 191