'The name 'nameof' does not exist in the current context

I'm using VS 2013 with .Net Framework 4.6. I want to use new C# 6 features(For example nameof).But I couldn't found it.

enter image description here

Should I use VS 2015? Or higher .Net Framework?



Solution 1:[1]

yes you need to use Visual Studio 2015. It's not supported in VS 2013. Related link : https://msdn.microsoft.com/en-us/library/dn986596.aspx

Solution 2:[2]

In pre-VS2015 you can add the following class to your solution and replace all instances of nameof( with NameOf.nameof(() => as suggested in get name of a variable or parameter

using System;
using System.Linq.Expressions;

namespace ICSharpCode.SharpZipLib
{
    public class NameOf
    {
        public static String nameof<T>(Expression<Func<T>> name)
        {
            MemberExpression expressionBody = (MemberExpression)name.Body;
            return expressionBody.Member.Name;
        }
    }
}

Solution 3:[3]

Installing this nuget package fixed it for my project.

Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform

Solution 4:[4]

You will not be able to use C#6 in VS2013. The compiler will not understand the new C# 6 syntax you are trying to use. How can I add C# 6.0 to Visual Studio 2013?

Solution 5:[5]

If you are getting this error in Teamcity, make the following changes to build step to correct it.

  1. Open your MSBuild step in teamcity
  2. Change the MSBuild version to Microsoft Build Tools 2015
  3. Change the MSBuild ToolVersion to 14.0

Solution 6:[6]

I got this error when using DotNetFiddle, I just switched to .NET Core instead of .NET 4.7.X.

Solution 7:[7]

  1. In "Package Manager Console" execute "install-package Microsoft.Net.Compilers" with your respective project selected.
  2. Ensure "C# Language Level" is "C# 6.0" in your project settings.

Solution 8:[8]

If you get this error and are using the latest .Net it could be because you are using the name of something that isn't a .Net Type. Here's an example:

Original with need to replace HardCoded "BaseUrl".

IConfiguration settingsConfig = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Settings = Options.Create(new Settings
{
  BaseUrl = settingsConfig.GetSection("BaseUrl").Value
});

This causes the error and it doesn't work because the BaseUrl doesn't exist in context:

IConfiguration settingsConfig = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
Settings = Options.Create(new Settings
{
  BaseUrl = settingsConfig.GetSection(nameof(BaseUrl)).Value
});

You need to fully qualify or specify a .Net Type that exists, eg:

Settings = Options.Create(new Settings
{
  BaseUrl = settingsConfig.GetSection(nameof(Settings.BaseUrl)).Value
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 amit dayama
Solution 2 Community
Solution 3 General Grievance
Solution 4 Community
Solution 5 sree
Solution 6 Jorn.Beyers
Solution 7
Solution 8 Jeremy Thompson