3

I have a method and a class with the same name. In a case, the compiler understands that I am using the class name, but not in another case:

using System;
using DTO;

namespace DTO
{
    public class Foo
    {
        public string Bar { get; set; }
    }
}

namespace Tests
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }

        private void Foo()
        {
            var foo = new Foo // Ok
            {
                Bar = nameof(Foo.Bar) // Not ok
            };
        }
    }
}

Error:

CS0119 'Program.Foo()' is a method, which is not valid in the given context

I get the same error with a static property:

public class Foo
{
    public static string Bar = "Hello";
}

// ...

private void Foo()
{
    var bar = Foo.Bar; // Error
}

If the compiler understands in the context that the Foo in new Foo is a class, why cannot it understand that Foo in nameof(Foo.Bar) is also a class? This notation makes no sense if Foo is a method.

Boiethios
  • 38,438
  • 19
  • 134
  • 183
  • This is a guess, hence not adding as an answer, but `Foo.Bar` would suggest a static property, which `Bar` isn't and so not something the compiler sees as valid. `foo.Bar` (lowercase F) is an instance of `Foo`, so `Bar` is a valid, visible property. – Diado Aug 09 '18 at 08:00
  • @Diado I tried with a static property, it triggers an error as well. I can add this information to the question. – Boiethios Aug 09 '18 at 08:01
  • 1
    @Boiethios Did you try `nameof(foo.Bar)`, just out of interest? – Diado Aug 09 '18 at 08:02
  • @Diado It works (if I initialize the property after the creation of the object, of course) – Boiethios Aug 09 '18 at 08:04

1 Answers1

7

In the first case the compiler knows you mean the class because of the new keyword. What follows new has to be a type name.

In the second case, there is no such restriction: Foo can be any variable, member, field or type. (Note that if this should work at all, Bar needs to be a static property of class Foo).

But since the method Foo is in the closest scope, the compiler thinks you mean this method group, which has no member called Bar.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Do you have an example where one can use a method's property? – Boiethios Aug 09 '18 at 08:05
  • 2
    Now, for sure, the compiler guys could write complex heuristics for this, saying that "OK, it might not be (the method).Bar, since that looks odd, and we can go find that there is a type named Foo as well, so we'll use that instead which makes the code compile", the first and foremost reason they didn't do this is that they didn't do this. – Lasse V. Karlsen Aug 09 '18 at 08:06
  • @Boiethios no, that's what the error message says: you cannot access "members" of a method group, that doesn't make sense...don't mix it with members of a `MethodInfo` retrieved via reflection or a delegate. – René Vogt Aug 09 '18 at 08:08
  • @LasseVågsætherKarlsen Hum, that makes sense – Boiethios Aug 09 '18 at 08:09
  • 1
    @LasseVågsætherKarlsen I guess the reasons for not doing it are: 1) as developer you could be very suprised what is finally used instead of what you expected, 2) could be very expensive for very little benefit – René Vogt Aug 09 '18 at 08:10