Default (Visual Basic)Default (Visual Basic)

将属性标识为其类、结构或接口的默认属性。Identifies a property as the default property of its class, structure, or interface.

备注Remarks

类、结构或接口最多可以将其一个属性指定为默认属性,前提是该属性至少采用一个参数。A class, structure, or interface can designate at most one of its properties as the default property, provided that property takes at least one parameter. 如果代码在未指定成员的情况下对类或结构进行引用,则 Visual Basic 解析对默认属性的引用。If code makes a reference to a class or structure without specifying a member, Visual Basic resolves that reference to the default property.

默认属性可能会减少源代码中的字符,但会使代码更难以阅读。Default properties can result in a small reduction in source code-characters, but they can make your code more difficult to read. 如果调用代码不熟悉你的类或结构,则当它对类或结构名称进行引用时,该引用是否访问类或结构本身,或者默认属性,则不能确定这一点。If the calling code is not familiar with your class or structure, when it makes a reference to the class or structure name it cannot be certain whether that reference accesses the class or structure itself, or a default property. 这可能会导致编译器错误或细微的运行时逻辑错误。This can lead to compiler errors or subtle run-time logic errors.

通过始终使用Option Strict 语句将编译器类型检查设置为 On,可以稍微降低默认属性错误的几率。You can somewhat reduce the chance of default property errors by always using the Option Strict Statement to set compiler type checking to On.

如果打算在代码中使用预定义的类或结构,则必须确定它是否具有默认属性,如果是,则必须确定其名称。If you are planning to use a predefined class or structure in your code, you must determine whether it has a default property, and if so, what its name is.

由于这些缺点,你应考虑不要定义默认属性。Because of these disadvantages, you should consider not defining default properties. 为实现代码可读性,还应考虑始终显式引用所有属性,甚至是默认属性。For code readability, you should also consider always referring to all properties explicitly, even default properties.

Default 修饰符可用于以下上下文:The Default modifier can be used in this context:

Property 语句Property Statement

另请参阅See also