Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda