Name

DefaultMember Attribute

Class

System.Reflection.DefaultMemberAttribute

Applies to

Class, Struct, or Interface

Description

Defines the default member of a structure, class, or interface. The default member is the member executed by the Type object’s InvokeMember method when a null string is supplied as the method’s name argument.

The Visual Basic .NET Default keyword is ultimately translated by the Visual Basic .NET compiler into the <DefaultMember> attribute. Visual Basic, however, requires that default members be parameterized. The use of the default member then allows you to specify a particular array element without having to explicitly reference the member. For instance, if the Items property is the default member of NewObject1, the statement

NewObject1.Items(10) = "Sleeping bag"

is functionally identical to

NewObject(10) = "Sleeping bag"

This works in VB.NET because the latter code statement is translated by the compiler into a call to the InvokeMember method that looks something like the following:

Dim t As Type = GetType(NewClass1)
Dim iFlags As BindingFlags = BindingFlags.Public Or _
                             BindingFlags.Instance Or _
                             BindingFlags.SetProperty
Dim arr(  ) As Object = { 10, "Sleeping bag" }
t.InvokeMember("", iFlags, Nothing, NewObject, arr)

Because the <DefaultMember> attribute, unlike the Default keyword, does not have to refer to a parameterized property, you can use the <DefaultMember> attribute to define default members that are not parameterized. However, this does not allow ...

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.