This project is read-only.
1

Closed

var @nameof = ...; nameof(@nameof) fails

description

var @class = 123;
Console.WriteLine(nameof(@class));
this code outputs "class". that's ok.
var @nameof = 123;
Console.WriteLine(nameof(@nameof)); // CS0149: Method name expected
Expected: This code should output "nameof", but,
Actual: it causes an error CS0149.

Note: var @nameof = 123 could be compiled.

Environment: Microsoft (R) Visual C# Compiler version 1.0.0.41031 in Visual Studio 2015 Preview
Closed Nov 16, 2014 at 9:17 PM by nmgafter
That works because class is not a contextual keyword.

nameof is a contextual keyword. If you have an identifier by that name in scope, we don’t use the built-in nameof operator.

This snippet of code has a local variable named nameof. So it will not be treated as a keyword in the scope of that variable.

But in any case we should be more clear in the specification that nameof is a contextual keyword.

comments

nmgafter wrote Nov 13, 2014 at 5:58 PM

In reading the latest (v5) spec on nameof(.), it appears to be a keyword. The implementation notes hint that it is a contextual keyword, but the spec proper appears to treat it as a keyword. I believe the LDM decided that it is contextual; if there is a user-declared identifier “nameof” in scope then we do not treat it as a keyword.

I cannot justify the implementation’s behavior based on the spec, but I believe this is what we agreed upon. We need to fix the spec.

PauloMorgado wrote Nov 14, 2014 at 2:00 AM

All the time, I was under the impression that nameof was a contextual keyword, as it should be.

if this is legal:
class @typeof { }

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(typeof(@typeof));
    }
}
This should also be:
var @nameof = 123;
Console.WriteLine(nameof(@nameof));
I think you should fix whatever you need to fix to make it work.

nmgafter wrote Nov 15, 2014 at 12:25 AM

That works because typeof is not a contextual keyword.

nameof is a contextual keyword. If you have an identifier by that name in scope, we don’t use the built-in nameof operator.

This snippet of code has a local variable named nameof. So it will not be treated as a keyword in the scope of that variable.

But in any case we should be more clear in the specification that nameof is a contextual keyword.

iwanaga wrote Nov 16, 2014 at 6:53 AM

It may be necessary that nameof is a contextual keyword for backward compatibility, but, I found another ugly behaviour caused by combination of the nameof and the using static. The following code can be compiled successfully and makes a nonsense result.
using System;
using System.Linq; // using static class, not namespace (warning only, no error)
 
public class Program
{
    public static void Main()
    {
        var name = nameof(Main); // invocation of the nameof method in my System.Linq
        Console.WriteLine(name); // shows "nonsense result"
    }
}
 
namespace System
{
    public static class Linq
    {
        public static string nameof(Action x) => "nonsense result";
    }
}

nmgafter wrote Nov 16, 2014 at 9:14 PM

That works because typeof is not a contextual keyword.

nameof is a contextual keyword. If you have an identifier by that name in scope, we don’t use the built-in nameof operator.

This snippet of code has a local variable named nameof. So it will not be treated as a keyword in the scope of that variable.

But in any case we should be more clear in the specification that nameof is a contextual keyword.


** Closed by nmgafter 11/16/2014 1:14PM