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.