使用Visual Studio 2010(可能也包括2008),我注意到Intellisense将为枚举类型建议完全限定的命名空间。
例如,我可以编写如下代码:
element.HorizontalAlignment = HorizontalAlignment.Right;
element.VerticalAlignment = VerticalAlignment.Bottom;
但是当我尝试写它时,它建议我像这样写:
element.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
element.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
这些不必要的额外代码可能会累积起来,使代码难以阅读,我必须与Intellisense斗争才能避免它。
有什么原因吗?我能关掉它吗?我猜原因是枚举的名称与属性的名称相同。但这真的不是一个好的理由。
编辑:
这里是另一个示例,说明完全限定名称并非必需。
using SomeOtherNamespace;
namespace SomeNamespace
{
public class Class1
{
public Class2 Class2 { get; set; }
public Class1()
{
// These all compile fine and none require fully qualified naming. The usage is context specific.
// Intellisense lists static and instance members and you choose what you wanted from the list.
Class2 = Class2.Default;
Class2.Name = "Name";
Class2.Name = Class2.Default.Name;
Class2 = Class2;
}
}
}
namespace SomeOtherNamespace
{
public class Class2
{
public static Class2 Default { get; set; }
// public static Class2 Class2; (This throws an error as it would create ambiguity and require fully qualified names.)
// public static string Name { get; set; } (This also throws an error because it would create ambiguity and require fully qualified names.
public string Name { get; set; }
}
}