使用 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; }
}
}