为什么编译器发现这个含糊不清?

Pau*_*art 2 c# compiler-construction intellisense

在我的基类中,我有一个泛型方法(理想情况下,这将是一个属性,但你不能拥有泛型属性)和一个非泛型属性,两者都具有相同的名称:

protected static T CurrentUserId<T>()
{
    ...
}

protected static string CurrentUserId
{
    get
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,当我来使用它们中的任何一个时,intellisense报告它们之间的歧义.当然,base.CurrentUserId(没有parethesese)为编译器提供了足够的线索,我想调用非泛型属性?

有谁知道编译器为什么要与此斗争?提前致谢.

Rex*_*x M 9

括号不提供足够的信息,因为您并不总是使用它们.这就是为什么这不起作用:

delegate T MyDelegate<T>();

new MyDelegate(myClass.CurrentUserId)

//are we talking about the method or the property?
Run Code Online (Sandbox Code Playgroud)


Hen*_*man 5

如果你省略了泛型,比如:

public int Value { get;  set; }

public int Value()
{
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

您还会收到错误:Type ...已包含Value的定义

我可以想到一个存在明显冲突的案例:

MyDelegate foo = new MyDelegate(Value);
Run Code Online (Sandbox Code Playgroud)