AttributeCollection.Item[] Property

Definition

Gets the attribute with the specified index.

Overloads

Item[Int32]

Gets the attribute with the specified index number.

Item[Type]

Gets the attribute with the specified type.

Item[Int32]

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

Gets the attribute with the specified index number.

C#
public virtual Attribute this[int index] { get; }

Parameters

index
Int32

The zero-based index of AttributeCollection.

Property Value

The Attribute with the specified index number.

Examples

The following code example uses the Item[] property to print the name of the Attribute specified by the index number in a text box. Because the index number is zero-based, this code example prints the name of the second Attribute in a text box. It assumes button1 and textBox1 have been created on a form.

C#
private void PrintIndexItem() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);

    // Prints the second attribute's name.
    textBox1.Text = attributes[1].ToString();
 }

Remarks

The index number is zero-based. Therefore, you must subtract 1 from the numerical position of a particular Attribute to access that Attribute. For example, to get the third Attribute, you need to specify myColl[2].

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Item[Type]

Source:
AttributeCollection.cs
Source:
AttributeCollection.cs
Source:
AttributeCollection.cs

Gets the attribute with the specified type.

C#
public virtual Attribute? this[Type attributeType] { get; }
C#
public virtual Attribute this[Type attributeType] { get; }

Parameters

attributeType
Type

The Type of the Attribute to get from the collection.

Property Value

The Attribute with the specified type or, if the attribute does not exist, the default value for the attribute type.

Examples

The following code example gets the DesignerAttribute from the collection and prints its value. It assumes that button1 and textBox1 have been created on a form.

For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

C#
private void PrintIndexItem2() {
    // Creates a new collection and assigns it the attributes for button1.
    AttributeCollection attributes;
    attributes = TypeDescriptor.GetAttributes(button1);
 
    // Gets the designer attribute from the collection.
    DesignerAttribute myDesigner; 
    // You must supply a valid fully qualified assembly name here. 
    myDesigner = (DesignerAttribute)attributes[Type.GetType("Assembly text name, Version, Culture, PublicKeyToken")];
    textBox1.Text = myDesigner.DesignerTypeName;
 }

Remarks

If the attribute does not exist in the collection, this property returns the default value for the attribute type.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1