37

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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Arian
  • 12,793
  • 66
  • 176
  • 300
  • As far as I know, you cannot use C# 6 in VS2013. You will need to upgrade to 2015 –  Oct 05 '15 at 07:49
  • afaik, C# Language Feature releases go hand in hand with Visual Studio releases, and aren't backported. – Claies Oct 05 '15 at 07:49
  • You should still be able to use .Net Framework 4.6 in Visual Studio 2013, but as you already answered your own question, you need Visual Studio 2015 in order to use C# 6. – Auguste Oct 05 '15 at 07:56

9 Answers9

42

Yes, to use C# 6 features, you need a C# 6 compiler. Visual Studio 2013 doesn't have a C# 6 compiler. Visual Studio 2015 does.

There have been Roslyn pre-releases for Visual Studio 2013, but they don't implement the final C# 6: they actually don't implement all the features, and what they do implement is different from the final C# 6 behaviour.

  • 6
    I'm using VS2015, update 2, and I just started seeing this issue today. It used to work. EDIT: Turns out there was a stupid syntax error in my program, and Visual Studio got confused and decided that it was because `nameof doesn't exist in the current context`. -- Very strange. – BrainSlugs83 Sep 09 '16 at 21:41
  • @BrainSlugs83 - that was it for me too. A downstream syntax error (this was also in a static method, wonder if that's related) caused nameof() to not be recognized. – Nick Bauer Jul 19 '17 at 20:14
6

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

amit dayama
  • 3,246
  • 2
  • 17
  • 28
6

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;
        }
    }
}
Community
  • 1
  • 1
ajeh
  • 2,652
  • 2
  • 34
  • 65
6

Installing this nuget package fixed it for my project.

Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Paulj
  • 3,096
  • 3
  • 25
  • 30
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?

Community
  • 1
  • 1
4

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

Jorn.Beyers
  • 1,784
  • 2
  • 21
  • 26
2

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
sree
  • 2,287
  • 28
  • 26
0
  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.
Daniel PP Cabral
  • 1,604
  • 1
  • 13
  • 19
  • 1
    It's Microsoft.Net.Compiler**s**. Not Microsoft.Net.Compiler. (I can't edit solution below 6 characters) Additionally, after installation C# 6.0 still doesn't exists in the list. – Ercument Eskar Feb 06 '17 at 07:46
0

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
});
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321