注意,你甚至都不需要@
:
string myClass = "my-Class";
以上是完全正常的。
你說:
compiler should give warning.
應該嗎?其實沒有含糊之處。
考慮這個非常典型的場景:
public class MyRandom
{
public Random Random { get; private set; }
public MyRandom()
{
// Is this ambiguous? No--the left HAS to be the property;
// the right HAS to be the type name.
Random = new Random();
}
}
這真的不是罕見變量的名字也類型的名稱。只有當一些成員重疊時纔會有歧義。例如:
public class MyThreadPool
{
public static void Add(Thread thread)
{
Console.WriteLine("Called the static method.");
}
}
public class SomeOtherClass
{
public List<Thread> MyThreadPool { get; private set; }
public SomeOtherClass()
{
MyThreadPool = new List<Thread>();
}
public void DoSomethingAmbiguous()
{
// To me, it would make sense for the compiler to issue a warning here,
// as it seems rather ambiguous (to me at least). However, it doesn't,
// which tells me the behavior is defined somewhere in the spec (I'm too lazy
// to check).
MyThreadPool.Add(null);
}
}
嗷,我的眼睛!但+1,現在我也很好奇...... – Cameron 2011-04-08 01:53:09
編譯器爲什麼要給出警告?你在兩個完全不同的上下文中使用myClass,並且編譯器沒有歧義。 – 2011-04-08 01:57:11
區別在於上下文。解析器可以根據上下文來確定它的標記是對象還是類。 – 2011-04-08 02:05:39