Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 7 local function not working as expected and no errors being displayed

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

like image 306
Izzy Avatar asked Apr 03 '17 08:04

Izzy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

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.

like image 112
Evk Avatar answered Sep 19 '22 19:09

Evk