Version Used: https://github.com/dotnet/roslyn/commit/5fbdd5c567b854e9b5f6f1ea64944373a40efb26
Steps to Reproduce:
class C
{
public string Instance;
public static string Static;
void M()
{
_ = nameof(C.Instance); // OK
_ = nameof(C.Instance.Length); // ERROR
_ = nameof(C.Static); // OK
_ = nameof(C.Static.Length); // OK
}
}
class C<T>
{
public T Instance;
public static T Static;
void M()
{
_ = nameof(C<string>.Instance); // OK
_ = nameof(C<string>.Instance.Length); // ERROR
_ = nameof(C<string>.Static); // OK
_ = nameof(C<string>.Static.Length); // OK
}
}
Expected Behavior: No error.
Actual Behavior:
CS0120: An object reference is required for the non-static field, method, or property 'C.Instance'
I suspect this is not per spec.
I'm marking as a bug even though I'm not sure whether or not this is the correct behavior (it's either by design or a bug, so I figured this was the right label).
According to https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#nameof-expressions this should be legal. It is therefore a compiler bug.
According to https://github.com/dotnet/vblang/blob/master/meetings/2014/LDM-2014-10-23.md (Yes, C# design notes in the VB repo because that's where the meeting was held)... the compiler was correct until @MadsTorgersen wrote a spec that didn't exactly agree.
Having said that, I like what @MadsTorgersen wrote into the spec and I'm happy to make the compiler do that.
Fixed in https://github.com/dotnet/roslyn/pull/24761 for dev15.7.x.
Most helpful comment
Having said that, I like what @MadsTorgersen wrote into the spec and I'm happy to make the compiler do that.