The name 'nameof' does not exist in the cu

2019-06-14 21:20发布

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?

7条回答
Fickle 薄情
2楼-- · 2019-06-14 21:30

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;
        }
    }
}
查看更多
We Are One
3楼-- · 2019-06-14 21:32

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-06-14 21:32

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

查看更多
太酷不给撩
5楼-- · 2019-06-14 21:40

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?

查看更多
Summer. ? 凉城
6楼-- · 2019-06-14 21:41

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
查看更多
叼着烟拽天下
7楼-- · 2019-06-14 21:47
  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.
查看更多
登录 后发表回答